Adsence750x90

Sunday, September 20, 2009

DataTable to CSV or Text File in Download format

Download Reports in CSV or Text Fromat in C# ( C Sharp)

Some special situations programmers want to download user reports in .csv or .txt format from database. This code snippet is a solution for that situations. first to take data from database in DataTable. Below class file is helpful to overcome this problam

first Example this wrote in a separate class file.

use system.io;


public class DownloadReports
{
public DownloadReports()
{
}

public void TextFormat(DataTable objData,string filename)
{
MemoryStream mm = getStream(objData);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.txt;", filename));
HttpContext.Current.Response.BinaryWrite(mm.ToArray());
HttpContext.Current.Response.End();
}

public void ExcelFormat(DataTable objData, string filename)
{
MemoryStream mm = getStream(objData);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/Ms-Excel";
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.csv;", filename));
HttpContext.Current.Response.BinaryWrite(mm.ToArray());
HttpContext.Current.Response.End();
}

private System.IO.MemoryStream getStream(DataTable objData)
{
System.IO.MemoryStream mm = new MemoryStream();
if (objData.Rows.Count > 0)
{
StreamWriter sw = new StreamWriter(mm);
sw.AutoFlush = true;
sw.WriteLine(); sw.WriteLine();
foreach (DataColumn dcols in objData.Columns)
{
sw.Write(dcols.ColumnName.ToString() + ",");
}
sw.WriteLine();
sw.WriteLine();
foreach (DataRow drow in objData.Rows)
{
for (int i = 0; i < objData.Columns.Count; i++)
{
sw.Write(drow[i].ToString() + ",");
}
sw.WriteLine();
}
}
return mm;
}
}


2nd option wrote in ASPX.CS file


public class DataTableToCsvAndTxtFormat
{
public DataTableToCsvAndTxtFormat()
{
}
private MemoryStream GetStream()
{
DataTable objData = new DataTable();
MemoryStream mm = new MemoryStream();
DBHelper objDBHelper=new DBHelper ();
string cmd = string.Format("SELECT Column1,Column2,Column3,Column4 FROM Table");
objData = objDBHelper.GetReaderTable(cmd);
if (objData.Rows.Count > 0)
{
objData.Columns[0].ColumnName = "First Name";
objData.Columns[1].ColumnName = "Last Name";
objData.Columns[2].ColumnName = "Email";
objData.Columns[3].ColumnName = "Mobile Number";

StreamWriter sw = new StreamWriter(mm);
sw.AutoFlush = true;
sw.WriteLine(); sw.WriteLine();
foreach (DataColumn dcols in objData.Columns)
{
sw.Write(dcols.ColumnName.ToString() + ",");
}
sw.WriteLine();
sw.WriteLine();
foreach (DataRow drow in objData.Rows)
{
for (int i = 0; i < objData.Columns.Count; i++)
{
sw.Write(drow[i].ToString() + ",");
}
sw.WriteLine();
}
}
}
public void DownloadTextFormat()
{
MemoryStream mm = GetStream();
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("content-disposition", "attachment; filename=Reports.txt;");
Response.BinaryWrite(mm.ToArray());
Response.End();
}
public void DownloadCsvFromat()
{
MemoryStream mm = GetStream();
Response.Clear();
Response.ContentType = "application/Ms-Excel";
Response.AddHeader("content-disposition", "attachment; filename=Reports.csv;");
Response.BinaryWrite(mm.ToArray());
Response.End();
}
}

Wednesday, September 2, 2009

General page life Cycle Stages in ASP.NET

General page life Cycle Stages in ASP.NET
1: Page Request
2: Start
3: Page Initializations
4: Load
5: Validation
6: Postback event handling
7: Rendering
8: Unload

1: Page Request:

The Page Request occurs before the page life cycle start. When a page is requested by a user ASP.NET determine some common things
1: whether the page need to be parsed and compiled
2: whether a cached version of the page can be sent in response without running the current page

2: Start

In start, page properties like request and response are set. In this stage the page also determine whether the request is a Postback or new request and set IsPostBack Property.

3: Page Initializations

During page initialization, controls on page are available and each control set its uniqueid property is set and themes. ie control raise their Init ViewState is set in this stage.

4: Load

On load if the current request is Postback control property are loaded with information recoved from viewstate and control state


Control state: Control state, introduced in ASP.NET version 2.0, is similar to view state but functionally independent of view state. A page developer can disable view state for the page or for an individual control for performance. However, control state cannot be disabled. Control state is designed for storing a control's essential data (such as a pager control's page number) that must be available on postback to enable the control to function even when view state has been disabled. By default, the ASP.NET page framework stores control state in the page in the same hidden element in which it stores view state. Even if view state is disabled, or when state is managed using Session, control state travels to the client and back to the server in the page. On postback, ASP.NET deserializes the contents of the hidden element and loads control state into each control that is registered for control state.
Note:
Use control state only for small amounts of critical data that are essential for the control across postbacks. Do not use control state as an alternative to view state.


Control state FROM MSDN


5: Validation

Validate method of all validator control is called, which set the IsValid property of individual validator controls in the requested page.

6: Postback event handling

If the request is a Postback any events handlers are called. And save viewstate

7: Rendering

Before rendering viewstate is saved for the page and all control.during the rendering phase the page calls the Render method for each control.

8: Unload

Unload call after the page has been fully rendered,sent to the client and is ready to be discarded.