2

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();
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
MiziaQ
  • 271
  • 1
  • 5
  • 14
  • 2
    Verfy that (CheckBoxList)FindControl("project_members") finds your control. And that rdr["MemberID"] could not return null value from database. – Sergey Berezovskiy Feb 13 '11 at 17:11
  • Thanks lazyberezivsky - I had my checkboxlist within an accordionpane...got it working now. – MiziaQ Feb 13 '11 at 17:27
  • 1
    These kinds of issues are good place to practice troubleshooting kills! (I.e. debugger, closer stack-trace analysis and reasoning, local tests, divide-and-conquer/logging, etc.) –  Feb 13 '11 at 19:17

2 Answers2

3

Consider also refactoring your code to separate presentation logic and data access logic:

private void SelectProjectMembers(int projectId)
{
    CheckBoxList chkbx = (CheckBoxList)FindControl("project_members");
    // verify if chkbx found

    foreach (string memberId in GetMemberIdsFor(projectId))
    {
        ListItem memberItem = chkbx.Items.FindByValue(memberId);

        if (memberItem != null)
            memberItem.Selected = true;
    }
}

private IEnumerable<string> GetMemberIdsFor(int projectId)
{
    List<string> memberIds = new List<string>();
    string query = String.Format("SELECT MemberID FROM ProjectIterationMember WHERE ProjectIterationID IN (SELECT ProjectIterationID FROM Iterations WHERE ProjectID = '{0}')", projectId);

    // using statements will dispose connection, command and reader object automatically
    using (SqlConnection conn = new SqlConnection(GetConnectionString()))
    using (SqlCommand cmd5 = new SqlCommand(query, conn))
    {
        conn.Open();

        using (SqlDataReader rdr = cmd5.ExecuteReader())
        {
            while (rdr.Read())
                memberIds.Add(rdr["MemberID"].ToString());
        }
    }

    return memberIds;
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    +1 because this would have the additional affect of showing "where" the exception comes from (even if it's otherwise unrelated :-) –  Feb 13 '11 at 19:19
1

Either rdr["MemberID"] returns null or chkbx is null as the control couldn't be found. Try rdr[0] instead for the case, and in the second case, make sure your control is found.

Femaref
  • 60,705
  • 7
  • 138
  • 176