Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Bind a List Box to a Data Source


Message #1 by "Ajay Varghese" <reese@b...> on Fri, 12 Jan 2001 23:26:17 +0530
This is a multi-part message in MIME format.



------=_NextPart_000_0077_01C07CEF.10271A80

Content-Type: text/plain;

	charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable



Hi,

Could some one let me know how to bind a List box (not a Data List) to a 

Data source.

I am using the below given code currently. It keeps throwing an error 

Stating



"System.NotSupportedException: Invalid attempt to read when no data is 

present."



But I am sure that the Data Source is not empty 'cos I tried setting it 

to a DataGrid and it displayed the records within the Data Source. But 

it just doesnt work with a List Box.

Could someone let me know what I'm doing wrong.





  MyConnection =3D New 

SQLConnection("server=3DGanga;uid=3Dsa;pwd=3D;database=3DPM")

  Dim MyCommand As New SQLCommand("select [Project Id], [Project Title] 

from [Project Master]", MyConnection)

  MyConnection.Open()

  Dim DR as SQLDataReader

  MyCommand.Execute(DR)

  LstProject.DataSource =3D DR

  LstProject.DataTextField=3DDR("Project Title").Value

  LstProject.DataValueField=3DDR("Project Id").Value

  LstProject.DataBind()

  MyConnection.Close()



Thanks,

Ajay






Message #2 by "Allen Tse" <allenptse88@h...> on Fri, 12 Jan 2001 13:42:58 -0500
You should use SQLDataSetCommand to get the DataSet, then use DataSet to 

insert the item to the list box



DataSet oDs;

DataTable oDt;



// Get DataSet from your business component

......



oDt = oDs.Tables(0);



foreach (DataRow dr in oDt.Rows)

{

    ListBox.Item.Insert((int)dr["ID"], (string)dr["Name"]

}



Hope this will help you. It works for me



Allen Tse

Sr. Software Engieer

CCH LIS Corp.











>From: "Ajay Varghese" <reese@b...>

>Reply-To: "ASP+" <aspx@p...>

>To: "ASP+" <aspx@p...>

>Subject: [aspx] Bind a List Box to a Data Source

>Date: Fri, 12 Jan 2001 23:26:17 +0530

>

>Hi,

>Could some one let me know how to bind a List box (not a Data List) to a 

>Data source.

>I am using the below given code currently. It keeps throwing an error 

>Stating

>

>"System.NotSupportedException: Invalid attempt to read when no data is 

>present."

>

>But I am sure that the Data Source is not empty 'cos I tried setting it to 

>a DataGrid and it displayed the records within the Data Source. But it just 

>doesnt work with a List Box.

>Could someone let me know what I'm doing wrong.

>

>

>   MyConnection = New SQLConnection("server=Ganga;uid=sa;pwd=;database=PM")

>   Dim MyCommand As New SQLCommand("select [Project Id], [Project Title] 

>from [Project Master]", MyConnection)

>   MyConnection.Open()

>   Dim DR as SQLDataReader

>   MyCommand.Execute(DR)

>   LstProject.DataSource = DR

>   LstProject.DataTextField=DR("Project Title").Value

>   LstProject.DataValueField=DR("Project Id").Value

>   LstProject.DataBind()

>   MyConnection.Close()

>

>Thanks,

>Ajay

>

>

>---

>http://www.asptoday.com - the leading site for timely,

>in-depth information for ASP developers everywhere.




>



_________________________________________________________________

Get your FREE download of MSN Explorer at http://explorer.msn.com



Message #3 by "Michael Gaertner" <mgaert@b...> on Fri, 12 Jan 2001 13:59:57 -0500
I think there's a couple things:

1) no DR.Read(). That would account for no data.

2) SQLDataReaders return data one row at a time, so I think you might need

to put your List box assignments in a loop.



The following C# code works with a DataSet and a List Box. Granted, not

exactly what you asked:



  ListProject.DataSource=ds.Tables["Project"].DefaultView;

  ListProject.DataTextField="Project Title";

  ListProject.DataValueField="Project ID";

  ListProject.DataBind();



Good luck!



Michael





  -----Original Message-----

  From: Ajay Varghese [mailto:reese@b...]

  Sent: Friday, January 12, 2001 12:56 PM

  To: ASP+

  Subject: [aspx] Bind a List Box to a Data Source





  Hi,

  Could some one let me know how to bind a List box (not a Data List) to a

Data source.

  I am using the below given code currently. It keeps throwing an error

Stating



  "System.NotSupportedException: Invalid attempt to read when no data is

present."



  But I am sure that the Data Source is not empty 'cos I tried setting it to

a DataGrid and it displayed the records within the Data Source. But it just

doesnt work with a List Box.

  Could someone let me know what I'm doing wrong.





    MyConnection = New SQLConnection("server=Ganga;uid=sa;pwd=;database=PM")

    Dim MyCommand As New SQLCommand("select [Project Id], [Project Title]

from [Project Master]", MyConnection)

    MyConnection.Open()

    Dim DR as SQLDataReader

    MyCommand.Execute(DR)

    LstProject.DataSource = DR

    LstProject.DataTextField=DR("Project Title").Value

    LstProject.DataValueField=DR("Project Id").Value

    LstProject.DataBind()

    MyConnection.Close()



  Thanks,

  Ajay

Message #4 by "Allen Tse" <allenptse88@h...> on Fri, 12 Jan 2001 14:11:21 -0500
Hi,



    You should use SQLDataSetCommand to get the DataSet, then use Insert 

method to add the LisBox items.



DataSet oDs;

DataTable oDt;



// Get the DataSet from Business .Net Component

............



oDt = oDs.Tables(0);



foreach(DataRow dr in Odt.Rows)

{

     ListBox.Items.Insert((int)dr["ID"], (string)dr["Name'];

}

Message #5 by Susan Warren <swarren@m...> on Fri, 12 Jan 2001 09:56:42 -0800
Ajay --

 

Looks close to right.  DataTextField and DataValueField are string

properties that take the name of the field, not it's value.  Try this:

 

  LstProject.DataTextField="Project Title"

  LstProject.DataValueField="Project Id"

 

hth,

Susan


  Return to Index