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
 
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/
 
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

Similar Threads

DropDownList 1
System.Data.DataRow error 1
DropDownList 1
Help!! Can someone please check my code? 8
session 1
Prolem with parameter! 3
Dropdownlist prepopulation 7
xmldatareader? 3

Top