Thursday, 3 September 2015

Download Word, Pdf Files using Asp.Net

Download Word Document
string sourcefile = Server.MapPath("~/WordDoc/ " + filename + ".doc");
if (File.Exists(sourcefile))
{
System.IO.FileInfo downloadFile = new System.IO.FileInfo(sourcefile);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/odc";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", downloadFile.Name));
HttpContext.Current.Response.AddHeader("Content-Length", downloadFile.Length.ToString());
HttpContext.Current.Response.WriteFile(downloadFile.FullName);
//HttpContext.Current.Response.End();
//HttpContext.Current.Response.Close();
}
 In above example, we are downloading a .doc file.
 Here are some of the most common content types replace in above code:
  •  .htm, .html     Response.ContentType = "text/HTML";
  • .txt    Response.ContentType = "text/plain";
  • .doc, .rtf, .docx    Response.ContentType = "Application/msword";
  • .xls, .xlsx    Response.ContentType = "Application/x-msexcel";
  • .jpg, .jpeg    Response.ContentType = "image/jpeg";
  • .gif    Response.ContentType =  "image/GIF";
  • .pdf    Response.ContentType = "application/pdf";

Monday, 24 August 2015

Define Stored Procedure, advantages and when to use ?

Stored Procedures are pre-compile objects which are compiled for first time and its compiled format is saved which executes (compiled code) whenever it is called.

Purposes and advantages of stored procedures:
  • Manage, control and validate data
  • It can also be used for access mechanisms
  • Large queries can be avoided
  • Reduces network traffic since they need not be recompiled
  • Even though the stored procedure itself may be a complex piece of code, we need not write it over and over again. Hence stored procedures increases reusability of code
  • Permissions can be granted for stored procedures. Hence, increases security.
Determine when to use stored procedure to complete SQL Server tasks.
  • If a large piece of code needs to be performed repeatedly, stored procedures are ideal
  • When hundreds of lines of SQL code need to be sent; it is better to use stored procedure through a single statement that executes the code in a procedure, rather than by sending hundreds of lines of code over the network.
  • When security is required.


Monday, 10 August 2015

Redirect to new asp.net page and javascript alert

Response.Write("<script>alert('Javascript alert message box');</script>");
Response.AddHeader("REFRESH", "3; ./default.aspx);


Javascript alert in asp.net (In another way)

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Error_msg", "alert('" + ex.Message + "')", true);


SQL Server Tricks

Convert Integer to Decimal

select isnull(format(99,'N'),0) as [integer to decimal]

Convert Decimal  to Integer

select convert(int,150.6,0) as [Decimal to Integer]

Find Recent Created Stored Procedure, Date


SELECT name, crdate, refdate
FROM sysobjects
WHERE type = 'P'
ORDER BY refdate desc