1

On button1 click i can add check boxes to the GridView column,

        grid_student.DataSource = semDS;
        grid_student.DataBind();
        TemplateField tf = new TemplateField();
        grid_student.Columns.Add(tf);

        foreach (GridViewRow gr in grid_student.Rows)
        {
            CheckBox chk = new CheckBox();
            chk.ID = "chkSelected";
            chk.Checked=true;
            gr.Cells[0].Controls.Add(chk);
        }

But the problem is when i click on button2 it says that "object reference is not set to an instance of an object" code for button2

 foreach (GridViewRow grr in grid_student.Rows)
    {
        if (grr.RowType == DataControlRowType.DataRow)
        {
            //CheckBox checkB = new CheckBox();
           // checkB = grr.Cells[0].FindControl("chkSelected") as CheckBox;
            CheckBox checkB =(CheckBox)grr.Cells[0].FindControl("chkSelected");
            if (checkB.Checked)
            {
                Label78.Text = "True";//or whatever i want 
            }
        }
    }

actually i know this type of errors are mostly occurred when we are trying to take some null value into an object, but in this case i have added check boxes successfully.
please reply,
it would be very helpful for me.
thanks in advance.

Ejaz47
  • 145
  • 1
  • 12
  • did you debug to check where you getting this exception? i think one of the rows is empty which is causing this exception – Nikitesh Mar 03 '15 at 10:55
  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Mar 03 '15 at 11:04
  • in button1 event i m generating checkboxes for each row . and i can see it in my gridview. so the NullRefrenceException should not come in picture. – Ejaz47 Mar 03 '15 at 11:27
  • @EjazAnsari Because new column doesn't exist. – Aria Mar 03 '15 at 11:39
  • yes i got it,but now what should i do for this...i want to add this column dynamically in **code behind**.thanks – Ejaz47 Mar 04 '15 at 06:16

0 Answers0