locked
Error - Object reference not set to an instance of the Object RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code . ON below line it is giving above error . There are 2 records in dt

    lst.DataSource = dt;

    using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                {
                    SqlCommand cmd = new SqlCommand("Select Location from [Loc]", con);
                    cmd.CommandType = CommandType.Text;
                    con.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        CheckBoxList lst = (CheckBoxList)e.Row.FindControl("ChekcBoxList1");
                        lst.DataSource = dt;
                        lst.DataBind();
                    }
                }

    Thanks

    Wednesday, August 8, 2018 12:04 PM

All replies

  • User-82109136 posted

    The following line is returning null

    CheckBoxList lst = (CheckBoxList)e.Row.FindControl("ChekcBoxList1");

    The F12 browser tools may be helpful in this case to find the correct ID to be used.

    Wednesday, August 8, 2018 1:14 PM
  • User-492460945 posted

    Hi Jagjit,

    It seems there is something wrong in the below line. Are you sure you have a checkbox control with id as "ChekcBoxList1"?

    CheckBoxList lst = (CheckBoxList)e.Row.FindControl("ChekcBoxList1");
    Wednesday, August 8, 2018 1:34 PM
  • User-82109136 posted

    Looks like a misspelling:

    ChekcBoxList1

    should be

    CheckBoxList1

    Wednesday, August 8, 2018 3:09 PM
  • User-1171043462 posted
    1. Always share Full function or event.
    2.  Share HTML
    Wednesday, August 8, 2018 8:37 PM