C# – inserting items to dropdownlist

asp.netcdata-binding

I want to display the items in my dropdownlist2 (from table 2 )based on the selected item of the dropdownlist1(from table 1) .

in the following, i just tried to select the lang column values from table2 based on the value which is in the dropdownlist1..((just to insert into the dropdownlist1))
is that correct code…?

    SqlDataAdapter da = new SqlDataAdapter(
       "Select lang from table2 whereheatre=@d",connect.con());
    da.SelectCommand.Parameters.AddWithValue("@d", DropDownList1.SelectedItem.Text);
    DataSet ds=new DataSet();
    da.Fill(ds,"l1");
    DropDownList2.Items.Add(ds);

is there any other way to do that…?

Best Answer

To add the new values into an existing dropdownlist, you should add the new rows manualy :

foreach (DataRow dr in ds.Tables[0].Rows)
{
    DropDownList2.Items.Add(new ListItem(dr["TextField"].ToString(), dr["ValueField"].ToString()));
}

Or, you should merge datatables before binding them to your dropdownlist.

Related Topic