i got error my code past below
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Data Source=HIMANSHU;Initial Catalog=try;Trusted_Connection=yes;");
SqlDataAdapter Sqlad = new SqlDataAdapter("SELECT * FROM gri", myConnection);
DataSet dsSupplier = new DataSet();
Sqlad.Fill(dsSupplier);
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=SupplierList.xls");
Response.ContentType = "application
/vnd.xls";
System.IO.StringWriter WriteItem = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlText = new HtmlTextWriter(WriteItem);
GridView1.RenderControl(htmlText);
Response.Write(WriteItem.ToString());
Response.End();
}
i got below error
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
.
plz help me
Lalit MPosted Nov 26, 2009, 11:55 PM
The above exception occurs when one tries to export a GridView control to Word, Excel, PDF, CSV or any other formats. Here the .net compiler thinks that the control is not added to the form and is rendered, hence it throws this error even if your GridView control is inside a form with runat = "server".
Solution
1. Tell the compiler that the control is rendered explicitly by overriding the VerifyRenderingInServerForm event. You can do that by adding the event to tha code behind file. Refer below
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
more info show this articles discus