I have a gridview which fetches its data from SQL server, The code that i have used is shown below using which i want to export the data into an excel sheet, but it throws the following exception
Server Error in '/' Application.
Control 'ctl00_m_g_e5bb414f_98f1_49bc_a614_59a3719c12e4_ctl00_BlogGridView' of type 'GridView' must be placed inside a form tag with runat=server.
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
BlogGridView.AllowPaging = false;
this.gridview();
BlogGridView.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in BlogGridView.HeaderRow.Cells)
{
cell.BackColor = BlogGridView.HeaderStyle.BackColor;
}
foreach (GridViewRow row in BlogGridView.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = BlogGridView.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = BlogGridView.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
BlogGridView.RenderControl(hw); <- Throws exception in this line
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
what shall i do