Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i have five tables in the database how can be i bind that tables to dropdownlist please can any one tell me ?
Posted
Comments
__TR__ 5-Sep-12 14:43pm    
Take a look at this thread
bind data to dropdownlist with c#[^]
Kuthuparakkal 5-Sep-12 22:24pm    
Why dont you google ? Do some stuff and then you post if you are stuck somewhere with the codes.

Hi,

Try this if could help...

C#
protected void BindProvince()
{
     List<province> lst = new List<province>();
     lst = cmd.GetLookupProvince();
     Province addl = new Province();
     addl.PROVINCE_ID = 0;
     addl.PROVINCE_NAME = "--Select One--";
     lst.Insert(0, addl);
     ddlProvince.DataValueField = "PROVINCE_ID"; 
     ddlProvince.DataTextField = "PROVINCE_NAME";
     ddlProvince.DataSource = lst;
     ddlProvince.DataBind();
}


Please vote if could help...

Regards,
 
Share this answer
 
v2
First you need to understand the flow of DropDownList[^]. It contains two important properties:
1. DataValueField
2. DataTextField

For the DataValueField and DataTextField properties, if you only set value to one of them, the other property is also set to the same value. If you only specify DropDownList1.DataTextField = "CategoryName", both the display text and value of each DropDownList item are set to Name. If you only specify DropDownList1. DataValueField = "CategoryID", both the display text and value of each DropDownList item are set to ID.
See this:
ASP.NET
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
</asp:dropdownlist>

C#
protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=testing;Integrated Security=True;"))
    {
        SqlCommand cmd = new SqlCommand("select CategoryID,CategoryName from Categories",conn);

        DataTable dt = new DataTable();

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "CategoryName";
        DropDownList1.DataValueField = "CategoryID";
        DropDownList1.DataBind();
    }
}




--Amit
 
Share this answer
 
You can use following method for all of your tables:(If you want data of all five tables in one drop down)
1. Add all your tables to your DataSet.
2. use following code:
C#
foreach(Table yourTable in DataSet.Tables)
{
    foreach(DataRow row in yourTable.Rows)
    {
          ListItem item = new ListItem();
          item.Text=row["your column"].ToString();
          item.Value=row["your column"].ToString();
          yourDropDownList.Items.Add(item);
    }
}


If you want different tables in different DropDownList then use Solution 2 mentioned above.
 
Share this answer
 

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