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.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";

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

//Grid ID
gvDisplay.RenderControl(hw);

Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
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 asp.net you can only upload the file of 4mb size 

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

How to increase session timeout in asp.net


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

Generate your Machine Key 
it will be unique so generate your own key from the below link and 
paste in web.config file


http://aspnetresources.com/tools/machineKey
 
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;
    }