[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-11-18
16
Medium Priority
?
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 41

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 41

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 41

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 41

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
 
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 41

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 41

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 41

Accepted Solution

by:
Kyle Abrahams earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

656 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