I am trying to export my GridView data into pdf however I am getting this error whenever I try to run it. Might be a stupid question but this is my first attempt on exporting data into pdfs.
This is the code
Edit* sorry I wasnt clear. Basically I am attempting this however I am getting the error that is in the screenshot whenever I attempt to run it
https://www.aspsnippets.com/Articles/Export-selected-checked-GridView-Rows-to-PDF-in-ASPNet.aspx
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["InvoiceID"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblClinicInvoices", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void btnPrint_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//Hide the Column containing CheckBox
GridView1.Columns[0].Visible = false;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//Hide the Row if CheckBox is not checked
row.Visible = (row.FindControl("chkselected") as CheckBox).Checked;
}
}
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}