Solved

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

Posted on 2016-11-18
16
21 Views
Last Modified: 2016-11-21
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.
0
Comment
Question by:Thunder724
  • 9
  • 7
16 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41893477
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
 
LVL 4

Author Comment

by:Thunder724
ID: 41893531
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
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41893536
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
 
LVL 4

Author Comment

by:Thunder724
ID: 41893543
You are correct it is not loaded.
the list is data bound also; I thought this would happen prior to the load event?
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41893559
You need the items to be loaded before you can set them as selected.
0
 
LVL 4

Author Comment

by:Thunder724
ID: 41893574
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
 
LVL 4

Author Comment

by:Thunder724
ID: 41893645
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
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41893664
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 4

Author Comment

by:Thunder724
ID: 41893684
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
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41893693
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
 
LVL 4

Author Comment

by:Thunder724
ID: 41893802
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
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41893809
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
 
LVL 4

Author Comment

by:Thunder724
ID: 41895969
I placed this in the Page_Load event with no success.
0
 
LVL 4

Author Comment

by:Thunder724
ID: 41896133
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
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 41896201
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
 
LVL 4

Author Closing Comment

by:Thunder724
ID: 41896225
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now