Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two dropdownlists in Asp.Net and a "Machines" table with two columns, "IPAddress" and "MachineName" in SQL Server. I am trying to extract data from both the columns in the table in SQL Server and put it in the dropdownlists. I was able to extract the "IPAddress" table only and put it in the first dropdownlist. I need to find out how to extract the data from the "MachineName" coluumn and put it in the second dropdownlist. Below I have the Page_Load with the codes for the first dropdownlist for reference.

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)

{
    con.Open();
    string s = "Select distinct IPAddress from Machines order by IPAddress";
    SqlDataAdapter da = new SqlDataAdapter(s, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DropDownList1.DataTextField = "IPAddress";
    DropDownList1.DataValueField = "IPAddress";
    DropDownList1.DataSource = ds;
    DropDownList1.DataBind();
    con.Close();
}
Posted
Updated 8-Jun-18 14:08pm
v2
Comments
[no name] 8-Jun-18 15:18pm    
See here:
Multiple Columns DropDown for ASP.NET[^]
copied from here
How to display multi-column in ASP.NET Dropdown List?[^]

and simply found with a quick google
Member 12270013 13-Jun-18 2:11am    
Firstly, where you are willing to bind both dropdowns? on pageload OR seperate method?

1) If Both are to to be binded on pageload : Change SQL query to bring both columns detail and then Use Linq to seperate it into lists and bind he lists to respective dropdowns.

2) If they are To be Binded seperately : Then You have straight forward answer. As you have made it for "IPAddress" Column, change that Column name to "MachineName".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900