Click here to Skip to main content
15,891,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,

Iam developing an asp.net application. Here in this code I am using a grid view and this is contain check boxes in its first column. So I want if I select the header column, then all the check boxes are selected and after button click all the selected data should inserted in the database. Here it works fine for select all checkboxes. But when I click on some perticular checkboxes for storing that perticular data it shows me this error. Here is my code and plz give the solution for it. How to inserted selected check boxes data in to the database from gridview.

C#
protected void btnAssign_Click(object sender, EventArgs e)
{
    string str = "";
    CheckBox cb = new CheckBox();
    CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkboxSelectAll");

    if (con.State == ConnectionState.Closed)
        con.Open();
    if (ChkBoxHeader.Checked == true)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            str = ((Label)GridView1.Rows[i].Cells[1].FindControl("lblRollNo")).Text;
            SqlCommand cmd = new SqlCommand("@
                insert into tbl_ExamDetails 
                    (Course,Stream,Subject,Date_of_Exam,RollNo,Room_No) 
                values (
                    '" + ddlCourse.SelectedValue
                    + "','" + ddlStream.SelectedValue
                    + "','" + ddlSubject.SelectedValue
                    + "','" + txtDate.Text
                    + "','" + str
                    + "','" + ddlRoom.SelectedItem.Value + "')", con);
            cmd.ExecuteNonQuery();
        }
    }
    //CheckBox chkCell = (CheckBox)GridView1.TemplateControl.FindControl("chkboxSelect");
    else if (ChkBoxHeader.Checked == false)
    {
        foreach (GridViewRow grv in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)GridView1.FindControl("chkboxSelect");
            if (chk.Checked==true)//Here it shows this error
            {
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    str = ((Label)GridView1.Rows[i].Cells[1].FindControl("lblRollNo")).Text;
                    SqlCommand cmd = new SqlCommand(@"
                        insert into tbl_ExamDetails
                            (Course, Stream, Subject, Date_of_Exam, RollNo, Room_No)
                        values (
                            '" + ddlCourse.SelectedValue
                            + "','" + ddlStream.SelectedValue
                            + "','" + ddlSubject.SelectedValue
                            + "','" + txtDate.Text
                            + "','" + str
                            + "','" + ddlRoom.SelectedItem.Value + "')", con);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }

    ChkSelectFalse();
    con.Close();
}
Posted
Updated 29-Jul-14 22:20pm
v3

Without knowing which line the error occurs on - and your error message will show you taht very clearly - we'd have to guess.

But this isn't a difficult problem for you to solve.
Use the debugger. Run your app.
When the problem occurs, the debugger will stop running your program and show you what happened and where.
Use the mouse (hover it over a variable to show it's content) and look for a value that contains null. When you find it, look back thorough your code to find out why it contains a null value. If that doesn't help, put a breakpoint on the first line of the method and run it again.
This time, step through your program, working out what should happen in advance and comparing that with what did happen. It should be fairly obvious what causes a null - normally it's a bad name, or a missing assignment.

This is a skill, and one you only really develop by using it. And this is a simple problem, so it's ideal for you to start learning on!
 
Share this answer
 
Set break points on these lines
C#
str = ((Label)GridView1.Rows[i].Cells[1].FindControl("lblRollNo")).Text;

and
C#
CheckBox chk = (CheckBox)GridView1.FindControl("chkboxSelect");


You will probably find that the return value from the method FindControl is null sometimes.

A better approach is is to do ,for example, like this:

C#
Control control = GridView1.Rows[i].Cells[1].FindControl("lblRollNo");
if (control != null)
  str = (control as Label).Text;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900