Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP!! Binding two column values to ASP.NET drop down list

Status
Not open for further replies.

richfield

IS-IT--Management
Jan 6, 2005
23
GB
Hi,

I've got a drop down list getting its values from a column in a dataset e.g Name.

code:

<asp:DropDownList id="ddlNames" runat="server" DataField="Id" Rows="1" DataTextField="Name" DataValueField="Price">


I need to change the Data Text field to display both Name and age.

So the drop down list may display

John
Paul
Andy

Is there a way to bind only the text value to Name and age also from the same dataset so it displays say

John 16
Paul 12
Andy 5

but the value remains the same as before?
 
I don't know about doing it from a dataset. I did this by concatinating a string in my SQL statement. This returned both values together. If you can't find another way then you could do it this way. I can post some code if you would like.
 

Code:
protected void LoadNames()
{
// Connection string stuff here
SqlConnection conn = new SqlConnection(yourConnectionString);
conn.Open();
string strSql="SELECT ID, Name, Age From yourTable";
SqlCommand cmd = new SqlCommand(strSql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
   ListItem li = new ListItem();
   li.Value = reader["ID"].ToString();
   li.Text = reader["Name"].ToString() + " - " + reader["Age"].ToString();
   ddlNames.Items.Add(li);
}
}
Let me know if you have any questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top