CheckboxList Object reference not set to an instance of an object.

I have a CheckBoxList object that is data bound.
When I load my page and want to assigne checked items based on my code return I get an error "Object reference not set to an instance of an object.".  it's almost like on load the object doesn't exist; but my textboxes are getting populated just fine.

So what i'm doing is pulling in a column with multiple values separated by a comma.  I then SPLIT these values into an array.

 SERVER_TYPES_STRING = Trim(drSQL.Item("SERVER_TYPE_IDS").ToString)
                    If Len(SERVER_TYPES_STRING) > 0 Then
                        Dim SERVER_TYPES() As String = Split(SERVER_TYPES_STRING.ToString, ",")
                        For S As Int16 = 0 To SERVER_TYPES.Length - 1
                            chklSERVER_TYPES.Items.FindByValue(Convert.ToInt16(SERVER_TYPES(S))).Selected = True
                        Next
                    End If

Open in new window


the following part of the code is where the error occurs:
chklSERVER_TYPES.Items.FindByValue(Convert.ToInt16(SERVER_TYPES(S))).Selected = True

Open in new window


I have also tried instead of the above line, another for each loop comparing the array elements to the values in the CheckboxList object.
LVL 4
Thunder724Asked:
Who is Participating?
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
Once again you would use the databound event, not  the load event.

Protected Sub CheckBoxList1_DataBound(sender As Object, e As EventArgs)
For Each x As ListItem In chklSERVER_TYPES.Items
                MsgBox(x.Value.ToString)
            Next
end sub

Open in new window

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
FindByValue can return null if you don't find the value.

so var x = chklSERVER_TYPES.Items.FindByValue(Convert.ToInt16(SERVER_TYPES(S)));
if (x ! =null)
  x.Selected = true;

Open in new window

0
 
Thunder724Author Commented:
Yes, that was helpful in that there are no errors.  I'm debugging the loop and see that the parsing of the string is correct.
So I'm getting, in one example an array of "4,6".  It loops through each value (e.g. first loop "4", second loop "6"; but the FindByValue is not working.  the list being evaluated is always nothing.  So, these numbers are the values of the item in the list, not the caption/names in the list.  is this correct?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Kyle AbrahamsSenior .Net DeveloperCommented:
Did you load (and databind if asp.net) the source for  chklSERVER_TYPES?

Can you set the breakpoint for
add the following lines temporarily to your code:
int x = chklSERVER_TYPES.Items.Count;
x =x ;  // set a breakpoint and evaluate x >0.  If it's 0 your list isn't loaded yet.

Open in new window

0
 
Thunder724Author Commented:
You are correct it is not loaded.
the list is data bound also; I thought this would happen prior to the load event?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
You need the items to be loaded before you can set them as selected.
0
 
Thunder724Author Commented:
Yes; but why does the binding not take place before the load event.  If I don't know that than I am going to have to load the data at the load event?
0
 
Thunder724Author Commented:
So, i'm trying to add the items manually instead of a data bind.
my code is clearly pulling the data and iterating through the checkboxlist; but nothing shows up in the list.  It's almost like this particular object does not get created in time of load event at all.  I did the same thing with a listdropdown and it works find.  Any suggestions?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
The order of events:

Get the data (manual, sql query, what have you).
Set the datasource
Bind the checkboxlist.

Attach a databound even to your checkbox list, and there you would process the selected items.
A good example can be found here:
http://meeraacademy.com/how-to-use-checkboxlist-ondatabound-event-in-asp-net-c/
0
 
Thunder724Author Commented:
Ok, I think something else is going on here.
So I created a button on the form.  After it loads I simply have the click event do the following:

ddpBACKUP_DEVICES.Items.Add("This is Rediculous!")

Open in new window


Nothing happens.  I can add items manually to the object but not dynamically at any point.  it's not my data binding and it's not the code I'm using; at lease the code looks sound and I see plenty of examples using the same.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
What if you add the item manually to the datasource and rebind it?

Also are you ajaxing that button?

Can you post your full code or setup a sample?

See this example as another reference:
http://www.4guysfromrolla.com/articles/050703-1.aspx
0
 
Thunder724Author Commented:
I'm Not using AJAX.
The below code works for other objects just fine, and if I place another object in this code it builds correctly.  I've also tried different iterations of the objects properties as well as a hard coded string.

cmdSQL = New SqlClient.SqlCommand
        Execute_SQL("ARDC_DATA_SERVER_TYPES_SELECT", 0)

        While drSQL.Read
            Dim CHKL As New ListItem
            CHKL.Text = drSQL.Item("SERVER_TYPE").ToString
            CHKL.Value = drSQL.Item("ITEM_KEY").ToString
            CHKL.Enabled = True
            CHKL.Selected = False
            ddpBACKUP_DEVICES.Items.Add(CHKL)
        End While

Open in new window

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Don't put them in manually:
 ddpBACKUP_DEVICES.DataSource = drSQL;
ddpBACKUP_DEVICES.DataTextField = "SERVER_TYPE";
ddpBACKUP_DEVICES.DataValueField =  "ITEM_KEY";
ddpBACKUP_DEVICES.DataBind();

Open in new window


Also, make sure you're not rebinding every time.  This should be done only once during the page load on the (!Page.IsPostBack) portion.
0
 
Thunder724Author Commented:
I placed this in the Page_Load event with no success.
0
 
Thunder724Author Commented:
So, I bound the SQL Connection to the control and again and the checkboxes are present; however I can't get access to the items on Load.
I placed the following code in the Load event just to see if I could iterate through it; but does not find anything.
For Each x As ListItem In chklSERVER_TYPES.Items
                MsgBox(x.Value.ToString)
            Next

Open in new window

0
 
Thunder724Author Commented:
Thanks Kyle, I understand what you meant about the Bound Event.
I'm very new to ASP; I've been writing client server apps since VB4 and find this very different especially because similar objects can be very different between the two.
0
All Courses

From novice to tech pro — start learning today.