Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want download grid view as pdf but when i click on button it is just refreshing page
but not downloading gridview as pdf




in catch block it showing exception as


System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.AbortInternal()
   at System.Threading.Thread.Abort(Object stateInfo)
   at System.Web.HttpResponse.AbortCurrentThread()
   at System.Web.HttpResponse.End()
   at User_WorkOrder.btnPrint_Click(Object sender, EventArgs e) in c:\Users\DELL\Desktop\Vinayak\VCHEM Current Updated\User\WorkOrder.aspx.cs:line 2553


What I have tried:

I have ASPX page as Follows

<asp:GridView ID="gvpdf" AutoGenerateColumns="False" runat="server" HeaderStyle-BackColor="dimgray" HeaderStyle-ForeColor="white" Width="100%" PageSize="100" Font-Size="15px" CellPadding="2" AllowPaging="True" BackColor="#F8F8F8" BorderStyle="None" OnRowDataBound="gvWorkOrder_RowDataBound">
                       <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                       <Columns>
                           <asp:BoundField HeaderStyle-Width="70px" HeaderText="Work_Order_Id" DataField="Work_Order_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide">
                               <FooterStyle BackColor="#CCCCCC" />
                               <HeaderStyle></HeaderStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                           </asp:BoundField>

                           <%--<asp:BoundField HeaderStyle-Width="150px" HeaderText="Package  Name" ItemStyle-HorizontalAlign="Center" DataField="PackageName" />--%>
                           <%--<asp:TemplateField HeaderText="Sr. No.">
                                                   <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                                                   <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                                                   <ItemTemplate>
                                                       <%#Container.DataItemIndex+1 %>
                                                   </ItemTemplate>
                                                   <ItemStyle Width="80px" />
                                               </asp:TemplateField>--%>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Sample No" DataField="Sample_No">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="200px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Sample Reg. No" DataField="Work_Order_No">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="200px" />
                           </asp:BoundField>

                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Customer Name" DataField="Customer_Name">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="300px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Date" DataField="Date">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Delivery Date" DataField="Delivery_Date">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="D Challan No" DataField="Customer_D_Challan_No">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="250px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Challan Date" DataField="Challan_Date">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="No of Sample" DataField="Quantity">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Name for Analysis Print" DataField="Name_for_Analysis_Print">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Remark" DataField="Remark">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="IsFerrous" DataField="IsFerrous" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:ButtonField ButtonType="Link" CausesValidation="false" HeaderText="Edit" ControlStyle-ForeColor="blue" ControlStyle-Width="50px" CommandName="Select" Text="Edit">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                           </asp:ButtonField>
                           <asp:TemplateField HeaderText="Delete">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                               <ItemTemplate>
                                   <asp:LinkButton ID="lnkDelete" OnClientClick="javascript:scroll(0,0);" runat="server" CommandName="cmdDelete">Delete</asp:LinkButton>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Customer Id" DataField="Customer_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />
                           <asp:TemplateField HeaderText="Print Work Order">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                               <ItemTemplate>
                                   <asp:LinkButton ID="lnkPrint" runat="server" CommandName="cmdPrint">Print</asp:LinkButton>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Print Internal Challan">
                               <ControlStyle ForeColor="Blue"></ControlStyle>
                               <ItemStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#CCCCCC" Width="50px" />
                               <ItemTemplate>
                                   <asp:LinkButton ID="lnkPrintchallan" runat="server" CommandName="cmdPrintChallan">Print</asp:LinkButton>
                               </ItemTemplate>
                           </asp:TemplateField>

                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="Uploaded_Doc" DataField="Uploaded_Doc">
                               <HeaderStyle Height="30px" HorizontalAlign="Center" VerticalAlign="Middle" CssClass="hidden"></HeaderStyle>
                               <ItemStyle Height="20px" HorizontalAlign="Left" VerticalAlign="Middle" CssClass="hidden" BorderColor="#CCCCCC" Width="100px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderStyle-Width="150px" HeaderText="IsNon_NABL" DataField="IsNon_NABL" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />

                       </Columns>

                       <EditRowStyle BorderColor="#CCCCCC" />

                       <HeaderStyle BackColor="DimGray" ForeColor="White"></HeaderStyle>

                       <PagerSettings PageButtonCount="5" />

                       <PagerStyle HorizontalAlign="Right" CssClass="GridPager" />
                   </asp:GridView>


aspx.cs page as follows


DataTable dt1 = new DataTable();
dt1 = (DataTable)ViewState["DataTable"];
gvpdf.DataSource = dt1;
gvpdf.DataBind();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=WorkOrder.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter s_w = new StringWriter();
HtmlTextWriter h_w = new HtmlTextWriter(s_w);
gvpdf.RenderControl(h_w);
gvpdf.HeaderRow.Style.Add("width", "15%");
gvpdf.HeaderRow.Style.Add("font-size", "10px");

gvpdf.Style.Add("text-decoration", "none");
gvpdf.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvpdf.Style.Add("font-size", "8px");

StringReader sr = new StringReader(s_w.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//  Paragraph title = new Paragraph("Delta Metallurgical Laboratory & Services", new Font(Font.FontFamily.TIMES_ROMAN, 14f, Font.BOLD));
//    title.Alignment = Element.ALIGN_CENTER;
//    pdfDoc.Add(title);
//    pdfDoc.Add(new Paragraph(" Unpaid Bill Report", new Font(Font.FontFamily.TIMES_ROMAN, 12f, Font.BOLD)));
//   pdfDoc.Add(new Paragraph("."));
htmlparser.Parse(sr);

pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
Posted
Updated 30-Aug-18 7:30am
v2
Comments
Matias Lopez 30-Aug-18 10:11am    
Maybe it's simple, but I ask you: Did you use GridView in an UpdatePanel? And the Button saves a PDF File with Threading? Can a Timeout be a problem?

Print to a "PDF PRINT DRIVER".

Can be done client (by the client); or server side.

The resulting file can be "picked up" from wherever for whatever.
 
Share this answer
 
 
Share this answer
 

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