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!!
 
Joined
Feb 28, 2008
Messages
1
Reaction score
0
Bazed Zero

is Because of Bazed_zero


news.microsoft.com said:
That did the trick.

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

Thanks!!


"Juno" <[email protected]> wrote in message
news:%23tN251%[email protected]...
> Hi,
>
> You have only 5 items in array, so do this:
> Dim liArray(4) As ListItem
>
> --
> Juno
> MCSD.NET, MCDBA, MCSE
> ----------------------------------------------------------
> Support Team of EasyDotNet, INC. http://www.EasyDotNet.com
> DataForm.NET - The most powerful data entry web server control for ASP.NET
>
>
>
> "news.microsoft.com" <[email protected]> wrote in message
> news:ueZ3Mc#[email protected]...
>> 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
>>
>>

>
>
 

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

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top