473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Nov 18 '05 #1
2 3962
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" <re*******@hotmail.com> wrote in message
news:ue*************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2
That did the trick.

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

Thanks!!
"Juno" <su*****@easydotnet.com> wrote in message
news:%2*****************@TK2MSFTNGP12.phx.gbl...
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" <re*******@hotmail.com> wrote in message
news:ue*************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DC Gringo | last post by:
I'm having such a problem with this DropDownList in a user control that is posting back and throwing an error: System.Web.HttpException: A DropDownList cannot have multiple items selected ...
1
by: Joe Delphi | last post by:
Hi, I am trying to populate dropdownlist items from the results of a database query. My code looks like this: Private Sub PopulateSubByBox(ByVal DBConn As SqlConnection) Dim DBComm As New...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
1
by: renor321 | last post by:
Hello, I've searched the groups unsuccessfully to the solution to this problem. I've seen a few others making note of it, but it didn't seem like there were any solutions. I have a drop down...
3
by: james | last post by:
Hi, I am trying to duplicate the contents of a combobox to another. I tried: cmbBoxA.Items.AddRange(cmbBoxB.Items); but it says it can't convert a combobox objectcollection to object. I...
1
by: Eugene Anthony | last post by:
ModifyUserRegistration.aspx --------------------------- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ModifyUserRegistration.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
11
by: xenoix | last post by:
hey there, im reasonably new to C# and im currently writing a backup application which im using as a learning resource. My PC :- Visual Studio 2005 .NET Framework 2 Component Factory Krypton...
27
by: user1980 | last post by:
hello, i am trying to create a request form using asp.net where when a user selects a checkbox, a dropdown should appear dynamically. I have seen few java-scripts in web, but I was wondering if I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.