472,806 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Adding to a list box in ASP .Net

Hi,
I am trying to add a dataset to a list box on an asp .net page, the
query results in about 20 rows coming back when I run it in access but when
I loop through the dataset adding each records field to the list box, the
list box only shows the first one and no others. Anyone know whats going on?

Thanks
Paul

Code ======================================

Imports System.Diagnostics

Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents lstModName As System.Web.UI.WebControls.ListBox

Const MODULENAME = "frmFilter.aspx.vb"

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

1: Dim conMain As New System.Data.OleDb.OleDbConnection()

2: Dim comMain As New System.Data.OleDb.OleDbCommand()

3: Dim rsData As System.Data.OleDb.OleDbDataReader

4: On Error GoTo ERRORHANDLER

10: conMain.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\GizmosDB.mdb;")

20: conMain.Open()

30: comMain.CommandText = "SELECT DISTINCT tblGizmos.[ModName] FROM
tblGizmos;"

40: comMain.CommandType = CommandType.Text

50: comMain.Connection = (conMain)

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)

70: While rsData.Read()

80: lstModName.Items.Add(rsData("ModName").ToString)

90: End While

100: rsData.Close()

End Sub

End Class

Jul 19 '05 #1
6 3416
Hi Paul,

Well, the following line of code could be the culprit:
60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow) Try placing it inside the while loop.

Secondly, why aren't you using a DataSet and a DataAdapter?

"Paul M." <pa**@nospam.fsnet.co.uk> wrote in message
news:bh**********@newsg1.svr.pol.co.uk... Hi,
I am trying to add a dataset to a list box on an asp .net page, the
query results in about 20 rows coming back when I run it in access but when I loop through the dataset adding each records field to the list box, the
list box only shows the first one and no others. Anyone know whats going on?
Thanks
Paul

Code ======================================

Imports System.Diagnostics

Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents lstModName As System.Web.UI.WebControls.ListBox

Const MODULENAME = "frmFilter.aspx.vb"

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

1: Dim conMain As New System.Data.OleDb.OleDbConnection()

2: Dim comMain As New System.Data.OleDb.OleDbCommand()

3: Dim rsData As System.Data.OleDb.OleDbDataReader

4: On Error GoTo ERRORHANDLER

10: conMain.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\GizmosDB.mdb;")

20: conMain.Open()

30: comMain.CommandText = "SELECT DISTINCT tblGizmos.[ModName] FROM
tblGizmos;"

40: comMain.CommandType = CommandType.Text

50: comMain.Connection = (conMain)

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)

70: While rsData.Read()

80: lstModName.Items.Add(rsData("ModName").ToString)

90: End While

100: rsData.Close()

End Sub

End Class

Jul 19 '05 #2
To Paul:

I think it's just your loop structure, i am not sure if that is the correct
while loop construct
I think the while loop should be something like
Do While ds.Read()
lstModName.Items.Add(rsData("ModName").ToString)
Loop
I see nothing else wrong with wat you did
To Harsh:
All he wanted to do is to fill up a list, datareader may in fact be faster
in this situation

Cheers

J

ps: i am no expert :)

"Paul M." <pa**@nospam.fsnet.co.uk> wrote in message
news:bh**********@newsg1.svr.pol.co.uk...
Hi,
I am trying to add a dataset to a list box on an asp .net page, the
query results in about 20 rows coming back when I run it in access but when I loop through the dataset adding each records field to the list box, the
list box only shows the first one and no others. Anyone know whats going on?
Thanks
Paul

Code ======================================

Imports System.Diagnostics

Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents lstModName As System.Web.UI.WebControls.ListBox

Const MODULENAME = "frmFilter.aspx.vb"

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

1: Dim conMain As New System.Data.OleDb.OleDbConnection()

2: Dim comMain As New System.Data.OleDb.OleDbCommand()

3: Dim rsData As System.Data.OleDb.OleDbDataReader

4: On Error GoTo ERRORHANDLER

10: conMain.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\GizmosDB.mdb;")

20: conMain.Open()

30: comMain.CommandText = "SELECT DISTINCT tblGizmos.[ModName] FROM
tblGizmos;"

40: comMain.CommandType = CommandType.Text

50: comMain.Connection = (conMain)

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)

70: While rsData.Read()

80: lstModName.Items.Add(rsData("ModName").ToString)

90: End While

100: rsData.Close()

End Sub

End Class

Jul 19 '05 #3
"Paul M." <pa**@nospam.fsnet.co.uk> wrote in message
news:bh**********@newsg1.svr.pol.co.uk...
I am trying to add a dataset to a list box on an asp .net page, the
query results in about 20 rows coming back when I run it in access but when I loop through the dataset adding each records field to the list box, the
list box only shows the first one and no others. Anyone know whats going on?

Yes, you're not reading the docs.
60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)


What does this line do ? Specifically CommandBehaviour.SingleRow ?
How do you think that might relate to your problem ?

The answer is here (sorry if the url wraps)
http://msdn.microsoft.com/library/en...DataCommandBeh
aviorClassTopic.asp

I
--

Jul 19 '05 #4
Bingo, changing:

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)

to

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleResult )

solves the problem.

Thanks to those who pointed it out!

Cheers
Paul M.

"Paul M." <pa**@nospam.fsnet.co.uk> wrote in message
news:bh**********@newsg1.svr.pol.co.uk...
Hi,
I am trying to add a dataset to a list box on an asp .net page, the
query results in about 20 rows coming back when I run it in access but when I loop through the dataset adding each records field to the list box, the
list box only shows the first one and no others. Anyone know whats going on?
Thanks
Paul

Code ======================================

Imports System.Diagnostics

Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents lstModName As System.Web.UI.WebControls.ListBox

Const MODULENAME = "frmFilter.aspx.vb"

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

1: Dim conMain As New System.Data.OleDb.OleDbConnection()

2: Dim comMain As New System.Data.OleDb.OleDbCommand()

3: Dim rsData As System.Data.OleDb.OleDbDataReader

4: On Error GoTo ERRORHANDLER

10: conMain.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\GizmosDB.mdb;")

20: conMain.Open()

30: comMain.CommandText = "SELECT DISTINCT tblGizmos.[ModName] FROM
tblGizmos;"

40: comMain.CommandType = CommandType.Text

50: comMain.Connection = (conMain)

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)

70: While rsData.Read()

80: lstModName.Items.Add(rsData("ModName").ToString)

90: End While

100: rsData.Close()

End Sub

End Class

Jul 19 '05 #5
Paul - The problem is indeed the loop syntax. Please see:
http://msdn.microsoft.com/library/de...asicbasics.asp

At the bottom you will see the following suggestion:
While...Wend loops can be nested to any level. Each Wend matches the most
recent While.

Tip The Do...Loop statement provides a more structured and flexible way to
perform looping.

There is no 'End While' in Visual Basic, and a search of MSDN returned no
results for 'End While'.

"Paul M." <pa**@nospam.fsnet.co.uk> wrote in message
news:bh**********@newsg1.svr.pol.co.uk...
Hi,
I am trying to add a dataset to a list box on an asp .net page, the
query results in about 20 rows coming back when I run it in access but when I loop through the dataset adding each records field to the list box, the
list box only shows the first one and no others. Anyone know whats going on?
Thanks
Paul

Code ======================================

Imports System.Diagnostics

Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents lstModName As System.Web.UI.WebControls.ListBox

Const MODULENAME = "frmFilter.aspx.vb"

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

1: Dim conMain As New System.Data.OleDb.OleDbConnection()

2: Dim comMain As New System.Data.OleDb.OleDbCommand()

3: Dim rsData As System.Data.OleDb.OleDbDataReader

4: On Error GoTo ERRORHANDLER

10: conMain.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\GizmosDB.mdb;")

20: conMain.Open()

30: comMain.CommandText = "SELECT DISTINCT tblGizmos.[ModName] FROM
tblGizmos;"

40: comMain.CommandType = CommandType.Text

50: comMain.Connection = (conMain)

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)

70: While rsData.Read()

80: lstModName.Items.Add(rsData("ModName").ToString)

90: End While

100: rsData.Close()

End Sub

End Class

Jul 19 '05 #6
Hello,
thanks for replying, I tried the loop syntax link you suggested
however it was invalid. As for your point about "end while" this is legal
syntax for vb .net, are you using an old version of MSDN?

Thanks
Paul M.

"peruser" <ja*******@hotmail.com> wrote in message
news:eq**************@TK2MSFTNGP12.phx.gbl...
Paul - The problem is indeed the loop syntax. Please see:
http://msdn.microsoft.com/library/de...us/vbcon98/htm
l/vbconpart1visualbasicbasics.asp
At the bottom you will see the following suggestion:
While...Wend loops can be nested to any level. Each Wend matches the most
recent While.

Tip The Do...Loop statement provides a more structured and flexible way to perform looping.

There is no 'End While' in Visual Basic, and a search of MSDN returned no
results for 'End While'.

"Paul M." <pa**@nospam.fsnet.co.uk> wrote in message
news:bh**********@newsg1.svr.pol.co.uk...
Hi,
I am trying to add a dataset to a list box on an asp .net page, the
query results in about 20 rows coming back when I run it in access but

when
I loop through the dataset adding each records field to the list box, the list box only shows the first one and no others. Anyone know whats going

on?

Thanks
Paul

Code ======================================

Imports System.Diagnostics

Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents lstModName As System.Web.UI.WebControls.ListBox

Const MODULENAME = "frmFilter.aspx.vb"

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

1: Dim conMain As New System.Data.OleDb.OleDbConnection()

2: Dim comMain As New System.Data.OleDb.OleDbCommand()

3: Dim rsData As System.Data.OleDb.OleDbDataReader

4: On Error GoTo ERRORHANDLER

10: conMain.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\GizmosDB.mdb;")

20: conMain.Open()

30: comMain.CommandText = "SELECT DISTINCT tblGizmos.[ModName] FROM
tblGizmos;"

40: comMain.CommandType = CommandType.Text

50: comMain.Connection = (conMain)

60: rsData = comMain.ExecuteReader(CommandBehavior.SingleRow)

70: While rsData.Read()

80: lstModName.Items.Add(rsData("ModName").ToString)

90: End While

100: rsData.Close()

End Sub

End Class


Jul 19 '05 #7

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

Similar topics

34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
5
by: Tim | last post by:
I have block of code adding values to dropdown list under if (!Page.IsPostBack) { adding items } else { refreshing data }
26
by: Simon Jefferies | last post by:
Hello, I am trying to add an item to a checked list box, like: clbList.Items.add("Hello",true) I get an error back: Run-time exception thrown: System.ArgumentOutOfRangeException -...
9
by: Kadett | last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control at run-time and filling it with some records (let's say 10 000). This operation seems to be quite fast, but when I call...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.