Tuman Sahu

Tuman Sahu

  • 1.5k
  • 293
  • 9.3k

getting error Document has no page when convert into pdf

Jan 3 2020 4:32 AM
Hi
I am using .net bind my data into gridview and wanted to convert grid data into pdf file.
but occure a error like Document has no page . My code is bellow .
  1. <form id="form1" runat="server">  
  2. <div>  
  3. <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"  
  4. RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"  
  5. runat="server" AutoGenerateColumns="false" OnPageIndexChanging="OnPageIndexChanging">  
  6. </asp:GridView>  
  7. <br />  
  8. <asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick = "ExportToPDF" />  
  9. </div>  
  10. </form>  
  1. Private Sub BindGrid()  
  2. Dim strConnString As String = ConfigurationManager.ConnectionStrings("safe").ConnectionString  
  3. Using con As New SqlConnection(strConnString)  
  4. Using cmd As New SqlCommand("select * from StudentDetails ")  
  5. Using sda As New SqlDataAdapter()  
  6. cmd.Connection = con  
  7. sda.SelectCommand = cmd  
  8. Using dt As New DataTable()  
  9. sda.Fill(dt)  
  10. GridView1.DataSource = dt  
  11. GridView1.DataBind()  
  12. End Using  
  13. End Using  
  14. End Using  
  15. End Using  
  16. End Sub  
  17. Protected Sub ExportToPDF(sender As Object, e As EventArgs) Handles btnExport.Click  
  18. Using sw As New StringWriter()  
  19. Using hw As New HtmlTextWriter(sw)  
  20. 'To Export all pages  
  21. GridView1.AllowPaging = False  
  22. Me.BindGrid()  
  23. GridView1.RenderControl(hw)  
  24. Dim sr As New StringReader(sw.ToString())  
  25. 'Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)  
  26. Dim pdfdoc1 As New Document(PageSize.A4, 2.0F, 2.0F, 2.0F, 0.0F)  
  27. Dim htmlparser As New HTMLWorker(pdfdoc1)  
  28. PdfWriter.GetInstance(pdfdoc1, Response.OutputStream)  
  29. pdfdoc1.Open()  
  30. pdfdoc1.NewPage()  
  31. htmlparser.Parse(sr)  
  32. pdfdoc1.Close()  
  33. Response.ContentType = "application/pdf"  
  34. Response.AddHeader("content-disposition""attachment;filename=GridViewExport.pdf")  
  35. Response.Cache.SetCacheability(HttpCacheability.NoCache)  
  36. Response.Write(pdfdoc1)  
  37. Response.[End]()  
  38. End Using  
  39. End Using  
  40. End Sub  
  41. Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging  
  42. GridView1.PageIndex = e.NewPageIndex  
  43. Me.BindGrid()  
  44. End Sub  
  45. Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)  
  46. End Sub  
Can any one help me to solve this error.

Answers (7)