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();
    }
}
3
  • 2
    Verfy that (CheckBoxList)FindControl("project_members") finds your control. And that rdr["MemberID"] could not return null value from database. Commented Feb 13, 2011 at 17:11
  • Thanks lazyberezivsky - I had my checkboxlist within an accordionpane...got it working now. Commented Feb 13, 2011 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.) Commented Feb 13, 2011 at 19:17

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 because this would have the additional affect of showing "where" the exception comes from (even if it's otherwise unrelated :-)
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.

1 Comment

What's the difference between accessing item by index or by name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.