Selected Item in ListBox

E

EdwardH

I am filling a ListBox from a stored proc and a SQLDataReader which works fine.

Private pCateg As SqlCommand
Private pDR as Data.Sql.SqlDataReader
pCateg = New SqlCommand("pLodge")
pCateg.CommandType = Data.CommandType.StoredProcedure
pDR = pCateg.ExecuteReader
ListBox1.DataSource = pDR
ListBox1.DataTextField = "Category"
ListBox1.DataValueField = "Category"
ListBox1.Databind

I am trying to use the Value/Text of the Selected Item but am failing to get
that value which is coming back as a Null:
ListBox1. SelectedValue

How should I test that an Item has been selected and then get the value for
further use?
With thanks
 
S

Stan

I am filling a ListBox from a stored proc and a SQLDataReader which works fine.

Private pCateg As SqlCommand
Private pDR as Data.Sql.SqlDataReader
pCateg = New SqlCommand("pLodge")
pCateg.CommandType = Data.CommandType.StoredProcedure
pDR = pCateg.ExecuteReader
ListBox1.DataSource = pDR
ListBox1.DataTextField = "Category"
ListBox1.DataValueField = "Category"
ListBox1.Databind

I am trying to use the Value/Text of the Selected Item but am failing to get
that value which is coming back as a Null:
ListBox1. SelectedValue

How should I test that an Item has been selected and then get the value for
further use?
With thanks

You are using the wrong type of object as a DataSource (It should
throw an exception).

Recommend a DataSet (one created in the App_Code folder as an XSD
file). Create a TableAdapter that uses the "pLodge" stored procedure
as its select command.
 
E

EdwardH

Many thanks, I realise I need the continuity of a Dataset. I have not
Datasets for a while, could you point me to a good article?
 
E

EdwardH

Have achieved the filling of the ListBox with a DataSet but still cannot get
value back from ListBox using - "ListBox1.SelectedValue.ToString".
Your help would be v welcome.
 
S

Stan

Have achieved the filling of the ListBox with a DataSet but still cannot get
value back from ListBox using - "ListBox1.SelectedValue.ToString".
Your help would be v welcome.







- Show quoted text -

I have to confess that I didn't realise that an SqlDataReader could be
used in that way until I tried it myself. I have since noticed though
in your code that you didn't include a statement to Open the db
connection.

Apart from that I cannot see any reason why you may be able to
populate the ListBox, make a selection in the browser, and then not
retrieve a selected value on postback. I've tried a similar scenario
in a dummy project of my own and all works fine.

Can I just check. When you say that you are getting a null value, how
are you initiating the postback, and what event are you placing the
code in to retrieve the SelectedValue?
 
E

EdwardH

I have decided to use a Gridview to include more columns of data which works
fine. Thanks for your input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top