title: i want to convert my gridview table into pdf
body: <p>**when i tried to convert my grid-view data into PDF i got null reference exception grid-view header row but data is available in grid view.please help me to solve this error. i am using asp.net web API in this.. and i tried to on another grid-view to bind data..it shows same error null reference exception but when i tried on direct button click it works fine..only it shows error when i tried on using web API..please help me to solve this
  i have attached my programming below data load and bind</p>
  
  <pre><code>public void GetData()
  
          {
  
              string conn = ConfigurationManager.ConnectionStrings["con"].ToString();
              SqlConnection con = new SqlConnection(conn);
              con.Open();
              SqlCommand cmd = new SqlCommand("select * from api", con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              da.Fill(ds, "api");
              //DataTable dta = new DataTable();
              //dta = ds.Tables["api"];
              //da.Fill(dta);
              GridView2.DataSource = ds;
              GridView2.DataBind();
  
  
          }
  
  
  convert to pdf
  
   public string Button3_Click()
          {
  
              PdfPTable pdfTable = new PdfPTable(GridView2.HeaderRow.Cells.Count);
  
              foreach (TableCell headercell in GridView2.HeaderRow.Cells)
              {
                  iTextSharp.text.Font font = new iTextSharp.text.Font();
                  font.Color = new Basecolor(GridView2.HeaderRow.ForeColor);
  
                  PdfPCell pdfcell = new PdfPCell(new Phrase(headercell.Text, font));
                  pdfcell.BackgroundColor = new Basecolor(GridView2.HeaderStyle.BackColor);
                  pdfTable.AddCell(pdfcell);
              }
              foreach (GridViewRow gridviewrow in GridView2.Rows)
              {
                  foreach (TableCell tablecell in gridviewrow.Cells)
                  {
  
                      iTextSharp.text.Font font = new iTextSharp.text.Font();
                      font.Color = new Basecolor(GridView2.RowStyle.ForeColor);
  
                      PdfPCell pdfcell = new PdfPCell(new Phrase(tablecell.Text));
                      pdfcell.BackgroundColor = new Basecolor(GridView2.RowStyle.BackColor);
                      pdfTable.AddCell(pdfcell);
                  }
              }
              Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
              PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
  
              pdfdoc.Open();
              pdfdoc.Add(pdfTable);
              pdfdoc.Close();
  
              Response.ContentType = "application/pdf";
              Response.AppendHeader("content-disposition", "attachment;filename=Employyes.pdf");
              Response.Write(pdfdoc);
              Response.Flush();
              Response.End();
  
              return "success";
  
          }
  
  ajax function call
  
    $(document).ready(function () {
              $("#Button2").click(function () {
                  alert("ajax call");
                  $.ajax
                  ({
  
                      method: "GET",
                      url: "api/Employees",
  
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
                      success: function (response) {
                          alert("success");
                      },
  
                      error: function (responce) {
                          alert("error");
                      },
  
  
                  });
              });
          });
   &lt;asp:Button ID="Button1" runat="server" Style="width: 136px" Text="Export to Excel" Width="175px" /&gt;&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp
            &lt;asp:Button ID="Button2" runat="server"  Text="Export to PDF" Width="175px" /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;div class="auto-style1" id="NewData"&gt;
              &lt;asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true"&gt;
              &lt;/asp:GridView&gt;
          &lt;/div&gt;
  here is my controller class
   public class EmployeesController : ApiController
      {
         // success s = new success();
  
          BusinessLogicLayer bal = new BusinessLogicLayer();
  
          public string Get(string username, string password)
  
          {
              return bal.login(username, password);
          }
  
  
          [System.Web.Mvc.HttpPost]
          public string POST(string firstname, string lastname, string username, string password, string email)
          {
              return bal.Register(firstname, lastname, username, password, email);
          }
           [System.Web.Http.HttpGet]
             public string buttonclick()
             {
                // MessageBox.Show("come");
                 return bal.ExportConvert();
  
          }
  
        here is my business logic layer class method
  
   public string ExportConvert()
          {
              success s = new success();
  
              return s.Button3_Click();
  
          }[enter image description here][1]
  </code></pre>
  
  <p>here my login page code</p>
  
  <pre><code>**&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;link href="Content/bootstrap.min.css" rel="stylesheet" /&gt;
  &lt;/head&gt;
  &lt;body style="padding-top:20px;"&gt;
  &lt;div class="col-md-10 col-md-offset-1"&gt;
  
      &lt;div class="well"&gt;
          &lt;table class="table table-bordered"&gt;
              &lt;thead&gt;
                  &lt;tr class="success"&gt;
  
  
  
                          &lt;th colspan="2"&gt;
                              Existing User Login
                      &lt;a class="btn btn-success pull-right" href="Registration.html"&gt;Register&lt;/a&gt;
                              &lt;/th&gt;
  
  
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
  
  
  
                  &lt;tr&gt;
                      &lt;td&gt;UserName&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" id="username"   placeholder="username"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;Password&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="password" id="password"   placeholder="Password" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr class="success"&gt;
  
                      &lt;td colspan="2"&gt;
                          &lt;input type="button" id="btnlogin"  class="btn btn-success" value="login" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
  
              &lt;/tbody&gt;
  
          &lt;/table&gt;
  
          &lt;div class="modal fade" tabindex="-1" id="successmodel"
               data-keyboard="false" data-backdrop="static"&gt;
              &lt;div class="modal-dialog modal-sm"&gt;
                  &lt;div class="modal-content"&gt;
  
                      &lt;button type="button" class="close" data-dismiss="modal"&gt;
                          &amp;times;
                      &lt;/button&gt;
                      &lt;h4&gt;Success&lt;/h4&gt;
                  &lt;/div&gt;
                  &lt;div class="modal-body"&gt;
                      &lt;h2&gt;Registraton success&lt;/h2&gt;
  
                  &lt;/div&gt;
                  &lt;div class="modal-footer"&gt;
                      &lt;button type="button" data-dismiss="modal" class="btn btn-success"&gt;
                          Close
  
                      &lt;/button&gt;
  
                  &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
          &lt;/div&gt;
  
      &lt;div id="divError" class="alert alert-danger collapse"&gt;
          &lt;a id="linkClose" class="close" href="#" &gt;&amp;times;&lt;/a&gt;
          &lt;div id="divErrorText"&gt;&lt;/div&gt;
      &lt;/div&gt; 
  
  
      &lt;/div&gt;
  
      &lt;script src="Scripts/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
      &lt;script src="Scripts/bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" &gt;
  
          $(document).ready(function () {
              $('#linkClose').click(function () {
                  $('divError').hide('fade');
              })
  
  
          });
  
          $('#btnlogin').click(function () {
              $.ajax({
  
                  url: '/api/Employees/?username=' + $("#username").val() + '&amp;password=' + $("#password").val(),
                  method: 'Get',
                  contentType: 'application/json',
                  data: { id: 1 },
                  dataType: 'json',
  
                  success: function (data) {
                      console.log(data);
                     //var data = data.d;
                     //alert("success " + data);
                     if (data == 'success') {
                         window.location = "/success.aspx";
                     }
                     else {
                         alert("Wrong Username and Password");
                     }
                    /*  if (data != null) {
                          $.each(data, function (i, obj) {
                              var c = new WorkflowsEntity(data[i]);    //logic
                              self.WorkflowList.push;
                          });
                          alert("success");
                          //window.location.href = "/success.html";
                      }*/
                  },
                  error: function (jqXHR, textStatus, errorThrown) {
                      alert(" un correct username password ");
                      $('#divErrorText').text(jqXHR.responceText);
                      $('#divError').show('fade');
                  }
              });
          });
  
  
      &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;**
  </code></pre>