Asked by:
Error in converting gridview to pdf

Question
-
User66371569 posted
My gridview is
<asp:GridView ID="GridView1" runat="server" Width="551px">
</asp:GridView>code is
Dim conTest As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("DBCS").ToString())
Dim dt As New DataTable
Dim cmaa2 As New SqlCommand("select empno ,empname ,nationality,operationid from Hpdcampsrmoves where moveid =" & "1" & "", conTest)
cmaa2.Connection = conTest
cmaa2.CommandType = CommandType.Text
conTest.Open()
If cmaa2.ExecuteNonQuery Then
Dim dr2 As SqlDataReader
dr2 = cmaa2.ExecuteReaderdt.Load(dr2)
GridView1.DataSource = dt
GridView1.DataBind()
GridView1.Visible = TrueconTest.Close()
Protected Sub btnExportPDF_Click(sender As Object, e As EventArgs) Handles btnExportPDF.Click
GridView1.AllowPaging = False
GridView1.DataBind()Dim bf As BaseFont = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + "\fonts\Arial.ttf", BaseFont.IDENTITY_H, True)
Dim table As New iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count)
Dim widths As Integer() = New Integer(GridView1.Columns.Count - 1) {}
For x As Integer = 0 To GridView1.Columns.Count - 1
widths(x) = CInt(GridView1.Columns(x).ItemStyle.Width.Value)
Dim cellText As String = Server.HtmlDecode(GridView1.HeaderRow.Cells(x).Text)'Set Font and Font Color
Dim font As New iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL)
font.Color = New Color(GridView1.HeaderStyle.ForeColor)
Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))'Set Header Row BackGround Color
cell.BackgroundColor = New Color(GridView1.HeaderStyle.BackColor)'Important for Arabic, Persian or Urdu Text
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
table.AddCell(cell)
Next
table.SetWidths(widths)For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
For j As Integer = 0 To GridView1.Columns.Count - 1
Dim cellText As String = Server.HtmlDecode(GridView1.Rows(i).Cells(j).Text)'Set Font and Font Color
Dim font As New iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL)
font.Color = New Color(GridView1.RowStyle.ForeColor)
Dim cell As New iTextSharp.text.pdf.PdfPCell(New Phrase(12, cellText, font))'Set Color of row
If i Mod 2 = 0 Then
'Set Row BackGround Color
cell.BackgroundColor = New Color(GridView1.RowStyle.BackColor)
End If'Important for Arabic, Persian or Urdu Text
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
table.AddCell(cell)
Next
End If
Next'Create the PDF Document
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
pdfDoc.Add(table)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.[End]()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End SubProtected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
End Sub
when I run and want to convert to pdf error appears in this line
Dim table As New iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count) " The number of columns in PdfPTable constructor must be greater than zero."
Wednesday, October 3, 2018 6:56 PM
All replies
-
User839733648 posted
Hi thepast,
when I run and want to convert to pdf error appears in this line
Dim table As New iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count)
" The number of columns in PdfPTable constructor must be greater than zero."
According to your description and code, I've made a sample and I tried to reproduce your problem succseefully.
The key point of the issue is the literal meaning of the error message.
The number of columns in PdfPTable constructor could not be zero or smaller than zero.
When GridView1.Columns.Count is 0, the error message will appear.
I suggest that you could debug your code in this line and watch the value of GridView1.Columns.Count.
If the value is 0, you may debug the the code of getting the database data to find if you have got the data successfully.
Best Regards,
Jenifer
Thursday, October 4, 2018 9:39 AM -
User66371569 posted
yesThursday, October 4, 2018 10:33 AM -
User66371569 posted
Yes Gridview apears with data the probem when i press button to convert to Pdf .
You can try my code just change query and seeThursday, October 4, 2018 10:33 AM -
User753101303 posted
Hi,
Comment all the PDF rendering codeto keep just :
GridView1.AllowPaging = False GridView1.DataBind()
The grid is still shown correctly or is it empty now ? If empty :
- either you need to set the source again and databind
- or you should do that in page_load once and keep the grid unchanged if this is a postback.Thursday, October 4, 2018 4:35 PM -
User66371569 posted
i put gridview code in page load and still convertion code give same error although griview shown dataThursday, October 4, 2018 8:11 PM -
User839733648 posted
Hi thepast,
I suggest that you could modify your GridView as following.
You have to add columns to your GridView. Otherwise it will appear the error message.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Eid" HeaderText="Eid" SortExpression="Eid" /> <asp:BoundField DataField="Ename" HeaderText="Ename" SortExpression="Ename" /> <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" /> <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" /> </Columns> </asp:GridView>
<asp:Button ID="btnExportPDF" runat="server" Text="Export to pdf" OnClick="btnExportPDF_Click" />You may see the value of "GridView1.Columns.Count" from the screenshots below.
not adding columns:
adding columns:
Best Regards,
Jenifer
Friday, October 5, 2018 2:00 AM -
User66371569 posted
If i add column in gridview
How can i query from database i want my gridview pull up data from database as shown upFriday, October 5, 2018 6:38 AM -
User839733648 posted
Hi thepast,
Dim cmaa2 As New SqlCommand("select empno ,empname ,nationality,operationid from Hpdcampsrmoves where moveid =" & "1" & "", conTest)According to your statement of getting value from database, you could add empno ,empname ,nationality,operationid the four columns to display.
You just could make small changes to the gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="empno" HeaderText="empno" SortExpression="empno" /> <asp:BoundField DataField="empname" HeaderText="empname" SortExpression="empname" /> <asp:BoundField DataField="nationality" HeaderText="nationality" SortExpression="nationality" /> <asp:BoundField DataField="operationid" HeaderText="operationid" SortExpression="operationid" /> </Columns> </asp:GridView>
Best Regards,
Jenifer
Friday, October 5, 2018 7:45 AM