Export gridview data to Excel document in asp.net C#


Follow the  steps involved :

Step 1:

Create a aspx page with a grid view control.

ViewExcelReport.aspx

aspx code

 Step 2:
Add the code for the button click in ViewExcelReport.aspx.cs

code

Now you are done, you can export the grid view in to Excel format.

if you’re getting any error message like

RegisterForEventValidation can only be called during Render();

To solve, add the overriding function VerifyRenderingInServerForm event in code. 

Thanks for reading this article!! please leave your comments and suggestion about this article.

 

Advertisements

http error 503. the service is unavailable in IIS after enabling 32 bit application


By default, IIS running on a 64 bit computer does not support running a 32 bit application. However, you can change the setting in an Application Pool under which the application is running by setting the ‘Enable 32-bit Applications‘ to true


In IIS, I created a new web-site and change the application pool to enable 32-bit application to support my 32 bit application.

While accessing the apppplication i got the  “HTTP Error 503. The service is unavailable” error.
Also Identified that the application pool was stopped repeatedly when i tried to access the  application. Finally got the below error from event viewer.

The Module DLL ‘C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\isapi\spnativerequestmodule.dll’ could not be loaded due to a configuration problem. The current configuration only supports loading images built for a x86 processor architecture. The data field contains the error number. To learn more about this issue, including how to troubleshooting this kind of processor architecture mismatch error, see http://go.microsoft.com/fwlink/?LinkId=29349.

Solution:
Run the following command in Windows Command Prompt. 


"%systemroot%\system32\inetsrv\AppCmd.exe" set config -section:system.webServer/globalModules
/[name='SPNativeRequestModule'].preCondition:integratedMode,bitness64

This  will solve the problem to load SPNativeRequestModule when the application pool is running in Integrated Mode and 64-bit application mode.
Hope the issue solved !!

Export gridview data to Excel document in asp.net C#


Follow the  steps involved :

Step 1: Create a aspx page with a grid view control.

 ViewExcelReport.aspx
<head id=”Head1″ runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td align=”right”>
<asp:ImageButton ID=”btnExcel” runat=”server”   onclick=”btnExcel_Click” />
</td>
</tr>
<tr>
<td>
<asp:GridView runat=”server” ID=”gridview1   DataSourceID=”databasedetails”  AllowPaging=”true” AllowSorting=”true” AutoGenerateColumns=”false”>
<RowStyle BackColor=”#EFF3FB” />
<FooterStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />
<PagerStyle BackColor=”#2461BF” ForeColor=”White” HorizontalAlign=”Center” />
<HeaderStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />
<AlternatingRowStyle BackColor=”White” />
<Columns>
<asp:BoundField DataField=”UserId” HeaderText=”UserId” />
<asp:BoundField DataField=”UserName” HeaderText=”UserName” />
<asp:BoundField DataField=”LastName” HeaderText=”LastName” />
<asp:BoundField DataField=”Location” HeaderText=”Location” />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<asp:SqlDataSource ID=”databasedetails” runat=”server” ConnectionString=”<%$ConnectionStrings:dbconnection %>
SelectCommand=”select * from Usertable”/>
</div>
</form>
</body>
</html>

Step 2:
Add the code for the button click in ViewExcelReport.aspx.cs

protected void btnExcel_Click_Click(object sender, ImageClickEventArgs e)
 

{

                      String strFileName=”Excel”;

             Response.Clear(); 


            Response.AddHeader(“content-disposition”,


            string.Format(“attachment;filename={0}.xls”, strFileName));


            Response.Charset = “”;

            Response.ContentType = “application/vnd.xls”;

            StringWriter stringWrite = new StringWriter();

            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            gridview.AllowPaging = false;

            BindSGrid();

            gridview.RenderControl(htmlWrite);

            Response.Write(stringWrite.ToString());

            Response.End();

            gridview.AllowPaging = true;

            BindGrid();

}

  

public override void VerifyRenderingInServerForm(Control control)

{

/* Confirms that an HtmlForm control is rendered for the specified ASP.NET

server control at run time. */

}



 

Now you are done , you can export the grid view in to Excel format.

if you’re getting any error message like 

RegisterForEventValidation can only be called during Render();

To solve, add the overriding function VerifyRenderingInServerForm event in code. 

Thanks for reading this article ! ! please leave your comments and suggestion about this article.
Thank you.
ramdsr26

Protected by Copyscape DMCA Copyright Protection

Validate EmailAddress in Asp.net using RegularExpressionValidator Control


Here are the steps to validate EmailAddress in Asp.net using RegularExpressionValidator Control.


Step 1 :
  Add a RegularExpressionValidator control to your web page.

Step 2: 

         Right click on the control and select properties.

    • Set the ControlToValidate property to indicate which control to validate.
    • Set the ValidationExpression property to  regular expression as shown
      in below image.