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