PC Review


Reply
Thread Tools Rate Thread

Adding to a list box in ASP .Net

 
 
Paul M.
Guest
Posts: n/a
 
      16th Aug 2003
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



 
Reply With Quote
 
 
 
 
Harsh Thakur
Guest
Posts: n/a
 
      16th Aug 2003
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." <(E-Mail Removed)> wrote in message
news:bhkk70$4ve$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
James Zhuo
Guest
Posts: n/a
 
      17th Aug 2003
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." <(E-Mail Removed)> wrote in message
news:bhkk70$4ve$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Iain Paton
Guest
Posts: n/a
 
      17th Aug 2003
"Paul M." <(E-Mail Removed)> wrote in message
news:bhkk70$4ve$(E-Mail Removed)...

> 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
--

 
Reply With Quote
 
Paul M.
Guest
Posts: n/a
 
      18th Aug 2003
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." <(E-Mail Removed)> wrote in message
news:bhkk70$4ve$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
peruser
Guest
Posts: n/a
 
      20th Aug 2003
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." <(E-Mail Removed)> wrote in message
news:bhkk70$4ve$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Paul M.
Guest
Posts: n/a
 
      22nd Aug 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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." <(E-Mail Removed)> wrote in message
> news:bhkk70$4ve$(E-Mail Removed)...
> > 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
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically extend a list when adding a new data to another list JB Microsoft Excel Misc 0 25th Mar 2010 02:50 PM
adding another item to a list of fields coming from a table inside of a list control thread Microsoft Access 8 28th Feb 2007 07:06 PM
Adding Contact and Distribution List Names to a List =?Utf-8?B?Q2xhcmtl?= Microsoft Outlook VBA Programming 1 4th Jan 2005 05:29 PM
OL2002: Adding list of contacts to a Distribution List after search PHiL M. Microsoft Outlook Contacts 9 15th Apr 2004 01:54 PM
Re: Adding file list to list or combo control Michael Daly Microsoft Access Form Coding 0 2nd Sep 2003 07:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:43 AM.