dropdownlist


E

Eugene Anthony

I have a table call category that has two fields
(CategoryID,Description). The code bellow will display two description
as added on the category table.

SqlConnection cnn = new
SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].Con
nectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = cnn;
myCommand.CommandText = "SELECT * FROM Category";
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Category");

DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataValueField = "CategoryID";
DropDownList1.DataTextField = "Description";
DropDownList1.DataBind();


However when I attempt to retrieve the CategoryID from the dropdownlist
control using DropDownList1.Text, I get the same CategoryID value as in:


CategoryID: 1 Description:Cars
CategoryID: 1 Description:House

rather than:

CategoryID: 1 Description:Cars
CategoryID: 7 Description:House


How do I solve the problem?

Your help is kindly appreciated.

Eugene Anthony
 
Ad

Advertisements

G

Guest

Hi

try instead

DropDownList1.SelectedItem.Value;
or
DropDownList1.SelectedItem.Text;

depending what you're looking for

--
-------------------------------------------
×× ×ª×©×•×‘×” זו עזרה לך, ×× × ×”×¦×‘×¢ "כן"

If my answer helped you please press "Yes" bellow

Adlai Maschiach
http://blogs.microsoft.co.il/blogs/adlaim/
 
G

Guest

nope , means that wasn't it ?
or nope , that's not what i'm looking for ( asked ) ?
--
-------------------------------------------
×× ×ª×©×•×‘×” זו עזרה לך, ×× × ×”×¦×‘×¢ "כן"

If my answer helped you please press "Yes" bellow

Adlai Maschiach
http://blogs.microsoft.co.il/blogs/adlaim/
 
Ad

Advertisements

D

David Wier

Are you binding the dropdown list in the Page_Load event?

Are you including it INSIDE an if/Then/Postback block?
 

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

Top