DropDownList.Items.AddRange(liArray) -- Error -- Why?

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

Hello,

Here is the problem. I have created an array of list items and I am adding
them to many DropDownList controls.

--------------The Code -------------------
Dim liArray(5) As ListItem
liArray(0) = New ListItem("First", "1")
liArray(1) = New ListItem("Second", "2")
liArray(2) = New ListItem("Third", "3")
liArray(3) = New ListItem("Fourth", "4")
liArray(4) = New ListItem("Last", "8")
DropDownList1.Items.AddRange(liArray) <--- Fails here!!
DropDownList2.Items.AddRange(liArray)
--------------------------------------------

When I debug I check the following.
? isNothing(liArray)
False
? isNothing(DropDownList1)
False
? isNothing(DropDownList2)
False

--------- Server Error ---------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 1068: liArray(3) = New ListItem("Fourth", "4")
Line 1069: liArray(4) = New ListItem("Last", "8")
Line 1070: DropDownList1.Items.AddRange(liArray)
Line 1071: DropDownList2.Items.AddRange(liArray)
Line 1072:

Why is this happening? The code is simple.

Thanks,
B
 
N

news.microsoft.com

That did the trick.

The strang part is that the code I used works without problems in C#.
Go figure.

Thanks!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top