Access more than 2 fields with Dropdown list

E

Elsa Luiz

Hi,
I would like to access the value of a third field of the query that I
used to populate my dropdown list. Is there a way to do that? For
example, in the following example I would like to access valueC.

SqlCommand sqlCmdLoad1 = new SqlCommand("SELECT valueA, valueB, valueC
FROM myTable", sqlConnection1);
sqlConnection1.Open();
SqlDataReader dr1 = sqlCmdLoad1.ExecuteReader();
ddl.DataSource = dr1;
ddl.DataTextField = "valueA";
ddl.DataValueField = "valueB";
ddl.DataBind();
dr1.Close();
sqlConnection1.Close();

I know that to access valueA I would use: ddl.SelectedItem.Text for
example, to access valueB I would use: ddl.SelectedValue, and to
access valueC, is there any way?

Thanks,
Elsa
 
T

Teemu Keiski

Hi,

on such cases you could create the value field to be one concatenated field
(that's done in SQL, separator could be say a comma) which is then assigned
to DataValueField property of DDL. When you access the value, you could get
what you need by splitting the value with comma.
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top