We help IT Professionals succeed at work.
Get Started

Populating Checkbox List Values from a Database

vcbertini
vcbertini asked
on
1,999 Views
Last Modified: 2012-05-10
I have built a checkbox List on my page using a datasource which builds the items from a data table.  There is a separate data table which holds the values of the checked list items.

For example, each partner can select multiple services they offer. Each service selection is stored in a map data table with the pertner ID and the service ID.  

I am trying to call up these partnerID/serviceID pairs to re-populate the checkbox list boxes when our partners log in to update their profiles, but I keep getting an error "Object reference not set to an instance of an object."  on this line of code from the first snipped below:

ListItem currentCheckBox = chkbx.Items.FindByValue(SOreader["AGENCY_SERVICES_INTERIM_ID"].ToString());

I'm not exactly sure what this error means and why I am getting it?  

Also, should I put a try/catch in place in case there are no records returned (i.e. the partner has not indicated any services they offer)?
// Build the Services Offered command
                SqlCommand cmdSO = new SqlCommand("sp_ServicesOffered_MAP", conn);
                cmdSO.CommandType = CommandType.StoredProcedure;
                cmdSO.Parameters.AddWithValue("@ID", partnerid);

                SqlDataReader SOreader = cmdSO.ExecuteReader();
                CheckBoxList chkbx = (CheckBoxList)Page.FindControl("cblServicesOffered");

                while (SOreader.Read()) 
                {     
                    ListItem currentCheckBox = chkbx.Items.FindByValue(SOreader["AGENCY_SERVICES_INTERIM_ID"].ToString());
                    if (currentCheckBox != null)
                    {
                        currentCheckBox.Selected = true;
                    }
                
                }

Open in new window

<p>
            <asp:Label ID="lblServicesOffered" runat="server" 
                AssociatedControlID="cblServicesOffered" Text="Services Offered" 
                CssClass="black12Bold"></asp:Label>
            <asp:CheckBoxList ID="cblServicesOffered" runat="server" BorderStyle="Groove" 
                RepeatColumns="3" TabIndex="60" DataSourceID="dsServicesOffered" DataTextField="LIST_ITEM_NAME" 
                DataValueField="LIST_ID">
            </asp:CheckBoxList>
        </p>

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 68 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE