I know there are several articles available for this error. Please do not refer me to those links, because I have already went through many and I am still unable to figure out the cause of this error. This is the code which I am using under button_click
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView2.AllowPaging = false;
GridView2.DataBind();
GridView2.RenderControl(hw);
GridView2.HeaderRow.Style.Add("width", "15%");
GridView2.HeaderRow.Style.Add("font-size", "10px");
GridView2.Style.Add("text-decoration", "none");
GridView2.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView2.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
I would like to inform you that I am using template field in my design and Below is the design view
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" Height="3px"
Width="2px" RowHeaderColumn="USN" AllowSorting="true" OnSorting="GridView2_Sorting">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="USN" HeaderText="Usn" SortExpression="USN" ReadOnly = "true"/>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly = "true"/>
<asp:TemplateField HeaderText="Test1">
<ItemTemplate>
<asp:TextBox ID="Testbox1" runat="server" Text='<%# Eval("T1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quiz1">
<ItemTemplate>
<asp:TextBox ID="Quizbox1" runat="server" Text='<%# Eval("Q1") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BorderColor="#660033" />
<RowStyle BackColor="#CCFFCC" BorderColor="#6600FF" BorderStyle="Double" />
</asp:GridView>
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
Text="Export To PDF" />