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 gives above error. In item.text it shows value

    chkBoxList0.Items.Add(item);

     CheckBoxList chkBoxList0 = (e.Row.FindControl("chkBoxList") as CheckBoxList);
                using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                {
                    SqlCommand cmd = new SqlCommand("Select Loc from [Location]", con);
                    cmd.CommandType = CommandType.Text;
                    con.Open();
    
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            ListItem item = new ListItem();
                            item.Text = sdr["Loc"].ToString();
                            chkBoxList0.Items.Add(item);
                        }
                    }
                    con.Close();
                }

    Thanks

    Friday, August 10, 2018 8:50 AM

All replies

  • User753101303 posted

    Hi,

    chkBoxList0 is likely null. Your control is perhaps named "chkBoxList0" as well rather than just "chkBoxList" ?

    Friday, August 10, 2018 9:01 AM
  • User-1716253493 posted

    Ensure it's datarow

            if (e.Row.RowType==DataControlRowType.DataRow)
            {
    
            }

    Friday, August 10, 2018 9:08 AM
  • User283571144 posted

    Hi JagjitSingh,

    According to your description, I couldn't directly find the reason why the it shows the Object reference not set to an instance of the object error.

    Could you please post more details codes about the which method you used to add the item and the girdview html makeup? E.g onrowdatabound

    If you could post more details information. it will be more easily for us to reproduce the issue and find the solution.

    Best Regards,

    Brando

    Monday, August 13, 2018 7:23 AM
  • User-492460945 posted

    Hi.

    Please post your HTML. It seems your checkbox id is spelt wrong.

    Tuesday, August 14, 2018 9:20 AM
  • User-2126206341 posted
    Make sure your database server engine is working and your connection is OK
    Wednesday, August 22, 2018 1:04 AM