Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to fetch data from database in dropdownlist..plz help me?
Posted
Comments
db7uk 22-Jun-12 4:17am    
Now this is far to general a question. What have you tried? What database are you referring to? What language are you using? All of this really helps because lack of information will mean lack of solution (or right solution). "Improve Question"

:doh:

Pick a good book or join a class to learn. If you cannot do either, just look for good learning resource on ASP.NET on web to start with. (Do tell me if you are unable to find good resource, I will share the links.)

For now, you would need to know a little about ADO.NET on how to fetch data from database. Once you know that, all you need to do is bind the datatable fetched to the dropdown and bind it.
Look at these:
Populate DropDown List from Database in ASP.NET & C#[^]
Binding DropDownList With Database in ASP.NET[^]
 
Share this answer
 
Comments
db7uk 22-Jun-12 18:55pm    
my 5. this is the best advice. one should try an learn / discover ways of doing this from the vast amount of material out there. if you are learning .net, i really recommend www.pluralsight.com. fantastic for any learning needs. also www.asp.net has a massive learning section which covers this sort of thing.
Hello,
As you have tagged your question with ASP.Net, I am assuming that you are using ASP.Net and C# as your code behind and according I am writing the code below. See if it helps you.

Lets consider that ddlData is the DropDownList defined in aspx page, tblCompanies is the table from which you want to fetch data.
C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      loadDropDown();
   }
}

public void loadDropDown()
{
   string strSelectString = @"select * from tblCompanies";
   SqlConnection conn = new SqlConnection("Server=SSK- PC\\MSSQL2008;database=*******;User ID=******;Password=*********;"); // Use your connection string here.
   conn.Open();
   SqlCommand cmd = new SqlCommand(strSelectString, conn);
   SqlDataAdapter adapter = new SqlDataAdapter(cmd);
   DataSet data = new DataSet();
   adapter.Fill(data);
   conn.Close();

   if(data.Tables[0]!= null && data.Tables[0].Rows.Count > 0)
   {
      ddlData.DataSource=data.Tables[0];
      ddlData.DataValueField = "CompanyId";
      ddlData.DataTextField = "CompanyName";
      ddlData.DataBind();
   }
}
 
Share this answer
 
v3
this is a simple example for bind a dropdownlist to datatable

C#
private void FillDropDownList(DataTable dt)
{
    ddl.DataSource = dt.DefaultView;
    ddl.DataValueField = "DataValueFieldName";
    ddl.DataTextField = "DataTextFieldName";
    ddl.DataBind();
    ListItem item = new ListItem("Select", "-1");
    ddl.Items.Insert(0, item);
}
 
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