November 19, 2013

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
<html xmlns="http://www.w3.org/1999/xhtml">
<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

March 1, 2013

Error: HTTP Error 500.22 - Internal Server Error

This error indicates that your application is not coded to run in Integrated Pipeline Mode.

To resolve this issue you must either:
  • Update your code to run in Integrated Pipeline Mode.
<system.webServer>
     <validation validateIntegratedModeConfiguration="false" />
 </system.webServer>
  • Open iis and select the application pool for your Site and change managed pipeline mode in to integrated to Classic.





Now its Cleared.

Protected by Copyscape DMCA Copyright Protection

HTTP Error 404.2 - Not Found


II7 error:
Error Summary
HTTP Error 404.2 - Not Found
The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server.

Follow the Below Steps:
1.       Double click the Server Name “xxxx”, then double click “ISAPI and CGI Restrictions
2.  Then select the version and select Allow from the right panel.














Now its Cleared.


January 25, 2013

How to add a New Eclipse Project to an SVN Repository


Adding a Project to SVN from Eclipse


Follow the below steps: 
1) Right-click the project and select Team -> Share Project.
















2) Select SVN as the repository type.







3) Select the repository this project should be stored in.









4) Select the repository directory where the project should be stored, and click Finish.









5) Now the /trunk/SimpleProject directory is created in the repository, and the project is associated with this repository location in Eclipse. You can now commit (upload) your changes by right-clicking and selecting Team -> Commit.
















When you’re done, the project will be linked with the repository