dropdownlist

  • Thread starter Thread starter Eugene Anthony
  • Start date Start date
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
 
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/
 
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/
 
Are you binding the dropdown list in the Page_Load event?

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