Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to fetch record from database and show it into dropdown.

What I have tried:

ddldropdown.SelectedIndex = ddldropdown.Items.IndexOf(ddldropdown.Items.FindByText(sdr["value"].ToString()));

is there any other method other then above method to fetch record from database to dropdown.
Posted
Updated 1-Sep-16 8:20am
v2
Comments
pparya27 29-Aug-16 1:18am    
The snippet you have given is for pre selecting an item in the dropdownlist.
Do you want to fetch the records from database and bind it to dropdownlist or to select an item in dropdownlist ??
Member 12662265 29-Aug-16 1:58am    
want to fetch the records from database and bind it to dropdownlist.
Patrice T 29-Aug-16 4:18am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

Its sample you can use for your issue

C#
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[dbConnection].ConnectionString;

SqlCommand comd = new SqlCommand();
comd.CommandText = "select distinct FIELD from TABLE";
comd.Connection = conn;
conn.Open();
SqlDataAdapter dap = new SqlDataAdapter(comd);
dap.Fill(ds2);
try
{
    for (int a = 0; a < ds2.Tables[0].Rows.Count; a++)
    {
        ddldropdown.Items.Add(ds2.Tables[0].Rows[a][0].ToString().ToUpper());
    }
}
catch (Exception ex)
{
    MessageBox.Show("" + ex.Message);
}
finally
{
    conn.Close();
}
 
Share this answer
 
v3

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