Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Selected Item in ListBox

Reply
Thread Tools

Selected Item in ListBox

 
 
EdwardH
Guest
Posts: n/a
 
      03-30-2008
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
 
Reply With Quote
 
 
 
 
Stan
Guest
Posts: n/a
 
      03-30-2008
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.
 
Reply With Quote
 
 
 
 
EdwardH
Guest
Posts: n/a
 
      03-30-2008
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 <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.
>

 
Reply With Quote
 
EdwardH
Guest
Posts: n/a
 
      03-31-2008
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.
>

 
Reply With Quote
 
Stan
Guest
Posts: n/a
 
      04-02-2008
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?
 
Reply With Quote
 
EdwardH
Guest
Posts: n/a
 
      04-07-2008
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 <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?
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
Listbox move multiple selected items to second listbox K B ASP .Net 2 01-08-2007 11:16 AM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
selected item and dropdown list/listbox rohith ASP .Net 3 08-29-2003 04:32 PM
Get the selected item from a listbox Craig Buchanan ASP .Net 0 06-26-2003 12:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57