0

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 */
}

}

The error

Rayy
  • 1
  • 1
  • 1
    This code has nothing to do with _exporting to pdf_ and the error is the classic NRE which has a canonical answer [here](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) please explain what you really want to do or we'll close as duplicate – Steve Mar 16 '19 at 18:07
  • ConfigurationManager.ConnectionStrings search your config file for a key with the name "InvoiceID". If there is no entry with this name it returns null and of course trying to read the ConnectionString property triggers the NRE. – Steve Mar 16 '19 at 18:20

0 Answers0