I am using the following code to check values previously added to a db table in a checkboxlist, but am getting an 'object reference not set to an instance of an object' error here:
ListItem currentCheckBox = chkbx.Items.FindByValue(rdr["MemberID"].ToString());
Here's the code, thanks for your help!
SqlDataReader rdr = null;
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd5 = new SqlCommand("SELECT MemberID FROM ProjectIterationMember WHERE ProjectIterationID IN (SELECT ProjectIterationID FROM Iterations WHERE ProjectID = '" + proj_id + "')", conn);
try
{
conn.Open();
rdr = cmd5.ExecuteReader();
CheckBoxList chkbx = (CheckBoxList)FindControl("project_members");
while (rdr.Read())
{
ListItem currentCheckBox = chkbx.Items.FindByValue(rdr["MemberID"].ToString());
if (currentCheckBox != null)
{
currentCheckBox.Selected = true;
}
}
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}