473,398 Members | 2,427 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,398 software developers and data experts.

DropDownList Data Binding

Hi,

I am attempting to bind a dropdown list to a SQLDataReader My code
looks like this:

Dim DBComm As New SqlCommand
Dim DBReader As SqlDataReader
DBComm.Connection = DBConn
DBComm.CommandText = "SELECT TESTER FROM PERSONNEL ORDER BY TESTER"
DBReader = DBComm.ExecuteReader()

'Bind the drop down list to the DBReader'
If DBReader.HasRows() Then
ddlstSubBy.DataSource = DBReader
ddlstSubBy.DataTextField = "TESTER"
ddlstSubBy.DataValueField = "TESTER"
ddlstSubBy.DataBind()
End If

The PERSONNEL database table has a single field named TESTER.

When the program attempts to execute the .DataBind() statement, I get an
exception message that says:

"Index 0 is not non-negative and below total rows count."

What does this mean? I know that the query returned 6 rows so the problem
is not that the SQLDataReader is empty.

Any help appreciated.
JD
Nov 19 '05 #1
3 1012
Hi,

and you have opened the db connection earlier in that code?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Nov 19 '05 #2
Yes, in fact I moved the SQLConnection opening to inside of this same
routine, as shown below, but I still get the exact same error message:

Private Sub PopulateSubByBox()
Dim DBConn As New SqlConnection
Dim DBComm As New SqlCommand
Dim DBReader As SqlDataReader
DBConn.ConnectionString = ConnStr
DBComm.Connection = DBConn
DBComm.CommandText = "SELECT TESTER FROM PERSONNEL ORDER BY TESTER"
DBConn.Open()
DBReader = DBComm.ExecuteReader(CommandBehavior.CloseConnecti on)

'Bind the drop down list to the DBReader'
If (DBReader.HasRows) Then
ddlstSubBy.DataSource = DBReader
ddlstSubBy.DataTextField = "TESTER"
ddlstSubBy.DataValueField = "TESTER"
ddlstSubBy.DataBind()
End If

DBReader.Close()
DBConn.Close()
End Sub

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

and you have opened the db connection earlier in that code?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Nov 19 '05 #3
Try changing the code to looping the DBReader.

While DBReader.Read()
Dim litem As New ListItem(DBReader("TESTER"))
ddlstSubBy.Items.Add(litem)
End While
DBReader.Close()

Normally the error indeed tries to say that there's not that data available,
so test it by chabnging the code to this. Does it now show the records?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Nov 19 '05 #4

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

Similar topics

18
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a...
4
by: wolfgang wagner | last post by:
hi all! after successfully integrating a dropdownlist in my datagrid i have another problem: i cannot set the selected index of the dropdownlistbox. here is my code: hardware.aspx...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.