SQL Server - populating dropdown with data from 2 tables

Asked By SVK N on 28-Dec-11 07:05 AM
i need to populate a dropdowm from 2 tables
table1 has name filed & table 2 has cname field

Riley K replied to SVK N on 28-Dec-11 07:22 AM

Do you want to show the items in table2 under the items in table1 in the dropdownlist ? If so, I think you can add the records of table2 into table1, then bind the table1 to the dropdownlist. You did not tell on what condition you want to show the data from two tables For example:

for(int i = 0; i < ds.Tables["Table2"].Rows.Count; i++)
   DataRow dr = ds.Tables["Table"].NewRow();
   dr["ID"] = ds.Tables["Table2"].Rows[i]["ID"];
   dr["Name"] = ds.Tables["Table2"].Rows[i]["Name"];
if (ds.Tables["Table"].DefaultView.Count > 0)
   ddlName.DataSource = ds.Tables["Table"].DefaultView;
   ddlName.DataTextField = "Name";
   ddlName.DataValueField = "ID,";
   ddlName.Items.Insert(0, new ListItem("Select", "0"));

Asked By SVK N on 28-Dec-11 07:38 AM
ok thnaks done
Jitendra Faye replied to SVK N on 28-Dec-11 08:13 AM

Use this code-

protected void GetDetails_Click(object sender, EventArgs e)

SqlConnection con = new SqlConnection("con string ");
SqlDataAdapter da;
string mySQL = "Write qury to join 2 tables ";
da = new SqlDataAdapter(mySQL, con);
DataSet ds = new DataSet();


//Now Filling Details-

DropList.DataSource = ds;
DropList.DataTextField = "colname";
DropList.DataValueField = "colname";

Try this code and let me know.