troubleshooting Question

NullReferenceException from Gridview? Button in Footer

Avatar of ayoZen
ayoZen asked on
C#Microsoft DevelopmentEditors IDEs
10 Comments1 Solution1290 ViewsLast Modified:
I have an application with a a few selections in drop-downs and a gridview.  The user selects criteria from the drop-downs to view data which is shown in a gridview.  i put a simple linkbutton in the footer, which gives the user the option to export the data from the search criteria to excel.  when I click this button, I get the following error:

Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.
Code: 0

Attached is my code that references the button click from the footer.  I even put the button outside of the gridview as a test (the sample code attached is test data), and I get the same error.  I am using Visual Stuido 2008.  
protected void TestExp_Click(object sender, EventArgs e)
    {
          string postdate = Convert.ToString(DateTime.Now.ToString("MMddyy_hhmmsstt"));

               Response.Clear();
               Response.Buffer = true;

               string dirName = @"C:\Delinquent Testing\";
               string strFilePath = "DelinquentAll" + "_" + postdate + ".xls";
               DirectoryInfo dInfo = new DirectoryInfo(dirName);
        
               HtmlForm hform = new HtmlForm();
               Response.AddHeader("content-disposition", "attachment; filename=" + strFilePath);
               Response.Charset = "";
               Response.ContentType = "application/ms-excel";

               if (!dInfo.Exists)
                   dInfo.Create();

               StringWriter sw = new StringWriter();
               HtmlTextWriter hw = new HtmlTextWriter(sw);

               GridView gv = new GridView();
               DataTable dt = new DataTable();

               string mc = ConfigurationManager.ConnectionStrings["SiteDB"].ConnectionString;
               SqlConnection conn = new SqlConnection(mc);

               conn.Open();

               SqlDataAdapter da = new SqlDataAdapter("select [Show Name], PrdNumber as [Show#], [30 Days], [60 Days], [90 Days], [120 Days], [150 Days], [180 Days]  from vw_delinquent Order by [Show Name] ASC", conn);
                 da.Fill(dt);

               Table table = new Table();
               table.GridLines = gv.GridLines;

               for (int i = 0; i < dt.Rows.Count; i++)
               {

                   gv.HeaderStyle.BackColor = Color.Gray;
                   gv.HeaderStyle.Font.Bold = true;
                   gv.Font.Size = 10;
               }
               gv.DataSource = dt.DefaultView;
               gv.DataBind();

               //add the headerrow to the table

               if ((((gv.HeaderRow) != null)))
               {
                   PrepareControlForExport(gv.HeaderRow);
                   table.Rows.Add(gv.HeaderRow);

               }
               // add each of the data rows to the table 
               foreach (GridViewRow row in gv.Rows)
               {
                   PrepareControlForExport(row);
                   table.Rows.Add(row);
               }
               // add the footer row to the table 
               if ((((gv.FooterRow) != null)))
               {
                   PrepareControlForExport(gv.FooterRow);
                   table.Rows.Add(gv.FooterRow);
               }

               table.RenderControl(hw);
               HttpContext.Current.Response.Write(sw.ToString());
               HttpContext.Current.Response.End();              
     
}
public static void PrepareControlForExport(Control control)
    {
        int i = 0;
        while ((i < control.Controls.Count))
        {
            Control current = control.Controls[i];
            if ((current is LinkButton))
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl(((LinkButton)current).Text));
            }
            else if ((current is ImageButton))
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl(((ImageButton)current).AlternateText));
            }
            else if ((current is HyperLink))
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl(((HyperLink)current).Text));
            }
            else if ((current is DropDownList))
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl(((DropDownList)current).SelectedItem.Text));
            }
            else if ((current is CheckBox))
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl(((CheckBox)current).Checked.ToString()));
            }
            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
            i = (i + 1);
        }
    }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros