Reputation: 3787
i have a problem with My dataGridVew
i am trying to export data from my DB to excel fie
it's a small page where there are only data grid view and button to export like this :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="SID" DataSourceID="SqlDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False"
ReadOnly="True" SortExpression="SID" />
<asp:BoundField DataField="G1Q1" HeaderText="G1Q1" SortExpression="G1Q1" />
<asp:BoundField DataField="G1Q2" HeaderText="G1Q2" SortExpression="G1Q2" />
<asp:BoundField DataField="G1Q3" HeaderText="G1Q3" SortExpression="G1Q3" />
<asp:BoundField DataField="G1Q4" HeaderText="G1Q4" SortExpression="G1Q4" />
<asp:BoundField DataField="G1Q5" HeaderText="G1Q5" SortExpression="G1Q5" />
<asp:BoundField DataField="G1Q6" HeaderText="G1Q6" SortExpression="G1Q6" />
<asp:BoundField DataField="G1Q7" HeaderText="G1Q7" SortExpression="G1Q7" />
<asp:BoundField DataField="G1Q8" HeaderText="G1Q8" SortExpression="G1Q8" />
<asp:BoundField DataField="G1Q9" HeaderText="G1Q9" SortExpression="G1Q9" />
<asp:BoundField DataField="G1Q10" HeaderText="G1Q10" SortExpression="G1Q10" />
<asp:BoundField DataField="G1Q11" HeaderText="G1Q11" SortExpression="G1Q11" />
<asp:BoundField DataField="G1Q12" HeaderText="G1Q12" SortExpression="G1Q12" />
<asp:BoundField DataField="G1Q13" HeaderText="G1Q13" SortExpression="G1Q13" />
<asp:BoundField DataField="G2Q1" HeaderText="G2Q1" SortExpression="G2Q1" />
<asp:BoundField DataField="G2Q2" HeaderText="G2Q2" SortExpression="G2Q2" />
<asp:BoundField DataField="G2Q3" HeaderText="G2Q3" SortExpression="G2Q3" />
<asp:BoundField DataField="G2Q4" HeaderText="G2Q4" SortExpression="G2Q4" />
<asp:BoundField DataField="G2Q5" HeaderText="G2Q5" SortExpression="G2Q5" />
<asp:BoundField DataField="G2Q6" HeaderText="G2Q6" SortExpression="G2Q6" />
<asp:BoundField DataField="G2Q7" HeaderText="G2Q7" SortExpression="G2Q7" />
<asp:BoundField DataField="G2Q8" HeaderText="G2Q8" SortExpression="G2Q8" />
<asp:BoundField DataField="G2Q9" HeaderText="G2Q9" SortExpression="G2Q9" />
<asp:BoundField DataField="G2Q10" HeaderText="G2Q10" SortExpression="G2Q10" />
<asp:BoundField DataField="G2Q11" HeaderText="G2Q11" SortExpression="G2Q11" />
<asp:BoundField DataField="G3P1Q1" HeaderText="G3P1Q1"
SortExpression="G3P1Q1" />
<asp:BoundField DataField="G3P1Q2" HeaderText="G3P1Q2"
SortExpression="G3P1Q2" />
<asp:BoundField DataField="G3P1Q3" HeaderText="G3P1Q3"
SortExpression="G3P1Q3" />
<asp:BoundField DataField="G3P1Q4" HeaderText="G3P1Q4"
SortExpression="G3P1Q4" />
<asp:BoundField DataField="G3P1Q5" HeaderText="G3P1Q5"
SortExpression="G3P1Q5" />
<asp:BoundField DataField="G3P1Q6" HeaderText="G3P1Q6"
SortExpression="G3P1Q6" />
<asp:BoundField DataField="G3P1Q7" HeaderText="G3P1Q7"
SortExpression="G3P1Q7" />
<asp:BoundField DataField="G3P1Q8" HeaderText="G3P1Q8"
SortExpression="G3P1Q8" />
<asp:BoundField DataField="G3P1Q9" HeaderText="G3P1Q9"
SortExpression="G3P1Q9" />
<asp:BoundField DataField="G3P1Q10" HeaderText="G3P1Q10"
SortExpression="G3P1Q10" />
<asp:BoundField DataField="G3P2Q1" HeaderText="G3P2Q1"
SortExpression="G3P2Q1" />
<asp:BoundField DataField="G3P2Q2" HeaderText="G3P2Q2"
SortExpression="G3P2Q2" />
<asp:BoundField DataField="G3P2Q3" HeaderText="G3P2Q3"
SortExpression="G3P2Q3" />
<asp:BoundField DataField="G3P2Q4" HeaderText="G3P2Q4"
SortExpression="G3P2Q4" />
<asp:BoundField DataField="G3P2Q5" HeaderText="G3P2Q5"
SortExpression="G3P2Q5" />
<asp:BoundField DataField="G3P2Q6" HeaderText="G3P2Q6"
SortExpression="G3P2Q6" />
<asp:BoundField DataField="G3P2Q7" HeaderText="G3P2Q7"
SortExpression="G3P2Q7" />
<asp:BoundField DataField="G3P2Q8" HeaderText="G3P2Q8"
SortExpression="G3P2Q8" />
<asp:BoundField DataField="G3P2Q9" HeaderText="G3P2Q9"
SortExpression="G3P2Q9" />
<asp:BoundField DataField="G3P2Q10" HeaderText="G3P2Q10"
SortExpression="G3P2Q10" />
<asp:BoundField DataField="G3P2Q11" HeaderText="G3P2Q11"
SortExpression="G3P2Q11" />
<asp:BoundField DataField="G3P3Q1" HeaderText="G3P3Q1"
SortExpression="G3P3Q1" />
<asp:BoundField DataField="G3P3Q2" HeaderText="G3P3Q2"
SortExpression="G3P3Q2" />
<asp:BoundField DataField="G3P3Q3" HeaderText="G3P3Q3"
SortExpression="G3P3Q3" />
<asp:BoundField DataField="G3P3Q4" HeaderText="G3P3Q4"
SortExpression="G3P3Q4" />
<asp:BoundField DataField="G3P3Q5" HeaderText="G3P3Q5"
SortExpression="G3P3Q5" />
<asp:BoundField DataField="G3P3Q6" HeaderText="G3P3Q6"
SortExpression="G3P3Q6" />
<asp:BoundField DataField="G3P3Q7" HeaderText="G3P3Q7"
SortExpression="G3P3Q7" />
<asp:BoundField DataField="G3P3Q8" HeaderText="G3P3Q8"
SortExpression="G3P3Q8" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HaronSurveyConnectionString %>"SelectCommand="SELECT * FROM [Results]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
'render html content to textwriter
GridView1.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.[End]()
End Sub
when i click on the button error message is coming : "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.???" even i have placed it in form ???
Upvotes: 3
Views: 35107
Reputation: 1064
Simply add this to your page
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
And put EnableEventValidation="false"
You can find it here
RegisterForEventValidation can only be called during Render
Upvotes: 0
Reputation: 1
This error also occurs when you bind datagrid using ENTITY FRAMEWORK. Use datatable instead.
Upvotes: -1
Reputation: 101
Just Write this Method:
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Upvotes: 10
Reputation: 3787
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ad As New results()
Dim dt As results.ResultsDataTable
dt = ad.Read()
Dim attachment As String = "attachment; filename=USurvey.xls"
Response.ClearContent()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/vnd.ms-excel"
Dim tab As String = ""
For Each dc As DataColumn In dt.Columns
Response.Write(tab + dc.ColumnName)
tab = vbTab
Next
Response.Write(vbLf)
Dim i As Integer
For Each dr As DataRow In dt.Rows
tab = ""
For i = 0 To dt.Columns.Count - 1
Response.Write(tab & dr(i).ToString())
tab = vbTab
Next
Response.Write(vbLf)
Next
Response.[End]()
'export to excel
End Sub
Upvotes: 0