Selected Item in ListBox

Discussion in 'ASP .Net Web Controls' started by EdwardH, Mar 30, 2008.

  1. EdwardH

    EdwardH Guest

    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
    EdwardH, Mar 30, 2008
    #1
    1. Advertising

  2. EdwardH

    Stan Guest

    On 30 Mar, 19:07, EdwardH <> 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.
    Stan, Mar 30, 2008
    #2
    1. Advertising

  3. EdwardH

    EdwardH Guest

    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?

    "Stan" wrote:

    > On 30 Mar, 19:07, EdwardH <> 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.
    >
    EdwardH, Mar 30, 2008
    #3
  4. EdwardH

    EdwardH Guest

    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 <> 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.
    >
    EdwardH, Mar 31, 2008
    #4
  5. EdwardH

    Stan Guest

    On 31 Mar, 17:43, EdwardH <> 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 <> 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?
    Stan, Apr 2, 2008
    #5
  6. EdwardH

    EdwardH Guest

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

    "Stan" wrote:

    > On 31 Mar, 17:43, EdwardH <> 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 <> 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?
    >
    EdwardH, Apr 7, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig Buchanan

    Get the selected item from a listbox

    Craig Buchanan, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,868
    Craig Buchanan
    Jun 26, 2003
  2. rohith
    Replies:
    3
    Views:
    5,982
    rohith
    Aug 29, 2003
  3. Iain
    Replies:
    3
    Views:
    928
  4. K B
    Replies:
    2
    Views:
    665
  5. mldardy
    Replies:
    0
    Views:
    963
    mldardy
    Sep 28, 2010
Loading...

Share This Page