locked
GridView Footer throwing Object reference error RRS feed

  • Question

  • User198251766 posted

    My web page has 2 drop down calendars for start and end dates as well as a drop down list. I have a text box binded to the footer of the gridview for a count of number of rows

    My issue I get sometimes is instead of showing the header if empty I'm getting a "Object reference not set to an instance of an object" error and I'm not really understanding why and how to fix it here is my code for the function where this happens:

    protected void Button1_Click(object sender, EventArgs e)
        {
            {
    
                DataTable dt = new DataTable();
                using (OleDbConnection conn = new OleDbConnection(GetDataConnection()))
                {
                    string querystring = "SELECT RepoDate, AlscoInvoice, WorkType, VinNum, OrderYear, OrderMake, OrderModel, Debtor, RepoAgent, PayOut FROM tblPersonnel WHERE (RepoDate >=@txtFrom AND RepoDate <=@txtTo) AND (RepoAgent =@ddlAgent) AND (WorkType='Involuntary' OR WorkType='Voluntary')";
                    
                    using (OleDbCommand command = new OleDbCommand(querystring, conn))
                    {
                        conn.Open();
                        command.Parameters.AddWithValue("@txtFrom", txtFrom.Text);
                        command.Parameters.AddWithValue("@txtTo", txtTo.Text);
                        command.Parameters.AddWithValue("@ddlAgent", ddlAgent.Text);
                        using (OleDbDataAdapter sqlDA = new OleDbDataAdapter(command))
                        {
                            sqlDA.Fill(dt);
                        }
                    }
                }
                if (dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                   
                }
                //counts total rows
                GridView1.ShowFooter = true;
                Label lblcount = new Label();
                lblcount.Text = GridView1.Rows.Count.ToString();
                GridView1.FooterRow.Cells[2].Controls.Add(lblcount); <--- This is where my error is coming from 
                
    
                decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("PayOut"));
                GridView1.FooterRow.Cells[7].Text = "Total Amount:";
                GridView1.FooterRow.Cells[7].HorizontalAlign = HorizontalAlign.Right;
                GridView1.FooterRow.Cells[8].HorizontalAlign = HorizontalAlign.Left;
                GridView1.FooterRow.Cells[8].Text = total.ToString("C0");
                GridView1.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Left;
                GridView1.FooterRow.Cells[1].Text = "Total Invoices:";
                GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
                txtBoxRepo.Text = lblcount.Text;
                
            }
        }

    <div data-url="https://forums.asp.net/post/set/1224/0/0" style="display: none;" id="link64_adl_tabid">28</div>

    20
    Thursday, August 20, 2015 11:29 PM

Answers

  • User-1716253493 posted

    AFAIK, footerrow available only when gridview1.rows.count > 0

    check it fisrt before getting control from footerrow

    if(GridView1.Rows.Count>0)
    {
                GridView1.FooterRow.Cells[2].Controls.Add(lblcount); 
    
    
    
    
    
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2015 3:20 AM

All replies

  • User-1716253493 posted

    AFAIK, footerrow available only when gridview1.rows.count > 0

    check it fisrt before getting control from footerrow

    if(GridView1.Rows.Count>0)
    {
                GridView1.FooterRow.Cells[2].Controls.Add(lblcount); 
    
    
    
    
    
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2015 3:20 AM
  • User198251766 posted

    Thanks for that, I missed that! I just needed to move the parenthesis down from my IF statement.

     if (dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                           
                //counts total rows
                GridView1.ShowFooter = true;
                Label lblcount = new Label();
                lblcount.Text = GridView1.Rows.Count.ToString();
                GridView1.FooterRow.Cells[2].Controls.Add(lblcount); 
    
                decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("PayOut"));
                GridView1.FooterRow.Cells[7].Text = "Total Amount:";
                GridView1.FooterRow.Cells[7].HorizontalAlign = HorizontalAlign.Right;
                GridView1.FooterRow.Cells[8].HorizontalAlign = HorizontalAlign.Left;
                GridView1.FooterRow.Cells[8].Text = total.ToString("C0");
                GridView1.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Left;
                GridView1.FooterRow.Cells[1].Text = "Total Invoices:";
                GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
                txtBoxRepo.Text = lblcount.Text;
               }

    55
    Saturday, August 22, 2015 7:22 AM