Saturday, September 17, 2011

Export Dataset to Excel in ASP.NET


Export grid to Excel. You can export the grid to excel sheet and give option to Save or Download
 the Excel Sheet.
In CS Page use the below code:
        DataClass objDataClass = new DataClass();
DataSet ds = objDataClass.GetDate();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.xls");
Response.Charset = "";
Response.ContentType = "application/";

System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

//Grid ID

In Aspx page use EnableEventValidation="false" in page directive

Increase number of connection to database

 Increase number of connection to database from ASP.NET

 Write below code in your web config file

<add key="ConnectionString" value="Data Source=servername;User ID=sa; Password=pass@123;
 Initial Catalog=databasename; Max Pool Size=400;" />

Maximum upload size in ASP.NET

How to increase the file upload size in Asp.Net

In you can only upload the file of 4mb size 

but you can increase the limit of upload file by using the below code
  <httpRuntime maxRequestLength="2097151" useFullyQualifiedRedirectUrl="true" executionTimeout="2600" />

How to increase session timeout in

Increase session timeout in ASP.NET by using this code. Put this code in web.config file. 
<!-- Increase session timeout  -->
<sessionState mode="InProc"  stateConnectionString="tcpip="  cookieless="false"   
This will increase the session timeout to 120 min. By default session timeout is 20 min 
    <sessionState mode="StateServer" stateNetworkTimeout="10" 
sqlCommandTimeout="30" cookieName="ASP.NET_SessionId" timeout="53200" 
    <httpRuntime executionTimeout="9999" maxRequestLength="10240"/>
    <machineKey validationKey="409B3A07F360A34A7C5DDD40B317AF7BBD7904A1E3D68273A9AC320BAE312D39619A28443AAF92E16A88BC66BF5AAF543C33723581EAD1378DEA9653C44868FC" 
validation="SHA1" decryption="AES" />

Generate your Machine Key 
it will be unique so generate your own key from the below link and 
paste in web.config file
in global.asax
 void Session_Start(object sender, EventArgs e) 
        // Code that runs when a new session is started
        // Code that runs when a new session is started
        Session.Timeout = 53200;