AZZA-KHAMEES
asked on
export grid to pdf
Hi experts
i am using obout grid, and i am trying to export grid to PDF, the export is working perfect when i fill the grid using sqldatasource but when i fill the grid using a stored procedure and function using code behind i am getting this error
i am attaching the code CustomerFeedbackByYear-v2.aspxCustomerFeedbackByYear-v2.aspx.cs
any suggestion
i am using obout grid, and i am trying to export grid to PDF, the export is working perfect when i fill the grid using sqldatasource but when i fill the grid using a stored procedure and function using code behind i am getting this error
i am attaching the code CustomerFeedbackByYear-v2.aspxCustomerFeedbackByYear-v2.aspx.cs
any suggestion
ASKER
thank you for the reply, i tried to do this but still the same problem
Which line is throwing the exception?
ASKER
i am getting error in
the grid have 8 columns, and the inner loop read them all and when it reach the end it add another column lik it read 9 column instead of 8 then i got error in the line mentioned above, i tried to write Grid1.Columns-1 but it gave me error
for (int i = 0; i < Grid1.Rows.Count; i++)
{
Hashtable dataItem = Grid1.Rows[i].ToHashtable();
foreach (Column col in Grid1.Columns)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dataItem[col.DataField].ToString(), font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
}
i am getting error in the inner loop in PdfPCell = new PdfPCell(new Phrase(new Chunk(dataItem[col.DataField].ToString(), font8)));
the grid have 8 columns, and the inner loop read them all and when it reach the end it add another column lik it read 9 column instead of 8 then i got error in the line mentioned above, i tried to write Grid1.Columns-1 but it gave me error
OK, so it doesn't sound like the issue is actually with the databinding, as you must have some data in your grid, otherwise it would have errored earlier.
When you run under the debugger what values do you get for Grid1.Rows.Count and Grid1.Columns.Count?
When you run under the debugger what values do you get for Grid1.Rows.Count and Grid1.Columns.Count?
Hi AZZA-KHAMEES;
I suspect that the exception is in this section of the line, dataItem[col.DataField].To String(), I believe that one or more columns in the row has a null value in it. Run the application and when the exception is thrown copy and paste this, dataItem[col.DataField].To String(), into the Watch window and see what its value is.
I suspect that the exception is in this section of the line, dataItem[col.DataField].To
ASKER
when i trace the code, the grid contain 8 columns but when i add watch it gave me 9 columns that why when the loop go through the data items it will be read correctly until it reach column 9 which is not available so the content will be null, i tried to add
but its wrong
foreach (Column col in Grid1.Columns - 1)
{
but its wrong
Try using:
foreach (Column col in Grid1.Columns - 1)
{
if (col != null && dataItem.ContainsKey(col.DataField))
{
// rest of your code
That should test that the column is not null, and that the hashtable contains a corresponding value before it attempts to use it.
ASKER
Can you post your code as it currently is? It sounds like something is screwy with the way your 3rd party control is working.
ASKER
this is the full code for the function
private void ExportGridToPDF()
{
MappingVariable();
DataTable dt = GetData(DirCode);
Grid1.DataSource = dt;
Grid1.DataBind();
// Stream which will be used to render the data
MemoryStream fileStream = new MemoryStream();
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
try
{
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter wri = PdfWriter.GetInstance(doc, fileStream);
doc.Open();//Open Document to write
//Font font8 = FontFactory.GetFont("TAHOMA", 7);
BaseFont bf = BaseFont.CreateFont("c:\\\\windows\\\\fonts\\\\tahoma.ttf", BaseFont.IDENTITY_H, true);
iTextSharp.text.Font font8 = new iTextSharp.text.Font(bf, 8, iTextSharp.text.Font.NORMAL);
//Write some content
Paragraph paragraph = new Paragraph("ASP.NET Grid - Export to PDF");
//Craete instance of the pdf table and set the number of column in that table
PdfPTable PdfTable = new PdfPTable(Grid1.Columns.Count);
PdfPCell PdfPCell = null;
//Add headers of the pdf table
foreach (Column col in Grid1.Columns)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(col.HeaderText, font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
//How add the data from the Grid to pdf table
for (int i = 0; i < Grid1.Rows.Count; i++)
{
Hashtable dataItem = Grid1.Rows[i].ToHashtable();
foreach (Column col in Grid1.Columns)
{
if (col != null && dataItem.ContainsKey(col.DataField))
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dataItem[col.DataField].ToString(), font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
}
}
PdfTable.SpacingBefore = 15f;
doc.Add(paragraph);
doc.Add(PdfTable);
}
catch (DocumentException docEx)
{
//handle pdf document exception if any
MessageBox.Show(docEx.ToString());
}
catch (IOException ioEx)
{
// handle IO exception
MessageBox.Show(ioEx.ToString());
}
catch (Exception ex)
{
// ahndle other exception if occurs
MessageBox.Show(ex.ToString());
}
finally
{
//Close document and writer
doc.Close();
}
// Send the data and the appropriate headers to the browser
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=oboutGrid.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(fileStream.ToArray());
Response.End();
}
I think it's being thrown off by the fact that the section that is generating the headers has an extra column, which is then causing the rows to offset since you don't explicitly create a new row.
Try changing:
Try changing:
//Add headers of the pdf table
foreach (Column col in Grid1.Columns)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(col.HeaderText, font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
To://Add headers of the pdf table
for (int i = 0; i < Grid1.Columns.Count - 1)
{
Column col = Grid1.Columns[i];
PdfPCell = new PdfPCell(new Phrase(new Chunk(col.HeaderText, font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
There is probably something about that extra column that you can use to identify that it isn't an actual data column, but I can't really offer any guidance on what it might be as I don't have your control library to play with.
ASKER
i changed it but still the same problem
Actually, change the -1 to a -2
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YESSSSS perfect finally, it worked :), thanks a lot for your help
ASKER
Thanks a lot :)
Try adding this line to the start of your ExportGridToPDF method:
Open in new window
If that works then you should also restructure the code so all of the binding stuff is done from a single method, rather than having to manually call a event handler.