On 31 Mar, 17:43, EdwardH <Edwa...@discussions.microsoft.com> wrote:
> 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.
>
>
>
> "Stan" wrote:
> > On 30 Mar, 19:07, EdwardH <Edwa...@discussions.microsoft.com> wrote:
> > > 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.- Hide quoted text -
>
> - 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?
|