Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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" />
Posted
Comments
Sergey Alexandrovich Kryukov 3-Mar-15 10:45am    
Your first requirement, "please don't refer me..." is incorrect, because we have no information on what references did you look at; and we don't know if you interpreted them correctly.
—SA
partha143 4-Mar-15 0:39am    
I am sorry for providing you insufficient information. I searched a lot, but I was unable to find any solution for this. It is my understanding that as I am using Item Template I need to make some changes in code (Please correct me if my understanding is wrong). Please reply back if you need more information. Thank you.
Szymon Roslowski 3-Mar-15 12:06pm    
Had the same issue this morning and seems like made a similar mistake.

This line
htmlparser.Parse(sr);

takes a StreamReader not a StringReader.

var stringStream = new StreamReader(GenerateStreamFromString(html));
htmlparser.Parse(stringStream);

(...)

private Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
partha143 4-Mar-15 0:49am    
Hello Szymon.
I replaced this
<pre>
htmlparser.Parse(sr);
</pre>
to this.
<pre>
var stringStream = new StreamReader(GenerateStreamFromString(sr.ToString()));
htmlparser.Parse(stringStream);
</pre>
The PDF file created and in the document this is what I got : System.IO.StringReader.
I am sorry but I am very much new to this and I know this is very silly. Please help
partha143 4-Mar-15 10:09am    
Anyone please help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900