Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to fill my dropdown list with certain values from the data base but i am having some problem with the codes can someone tell me where i am doing it wrong?

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)
{
     fillDDl();
}

void fillDDl()
{
    SqlConnection con = new SqlConnection("Data Source=MICKEY\\SQLEXPRESS;Initial Catalog=rr;Integrated Security=True");
    string query = "select * from rr.itemMaster";
    SqlCommand com = new SqlCommand(query, con);
    SqlDataReader sdr = new SqlDataReader();
    try
    {
       con.Open();
       sdr = com.ExecuteReader();
       while(sdr.Read())
       {
           string sName = sdr.GetString("itemName");
           olDDLItem.Items.Add(sName);
       }
    }
    catch(Exception ex)
    {
        olResult.Text = "successfull";
    }
}
Posted
Updated 3-May-16 5:41am
v2
Comments
Kats2512 14-Mar-16 2:26am    
refer to this link, it should be able to sort your issue as it shows what you need to do step by step. also you are not setting the value and text for the drop down list.

http://www.aspsnippets.com/Articles/Bind-Fill-Populate-DropDownList-control-from-database-in-ASPNet-using-C-and-VBNet.aspx
Member 12355460 14-Mar-16 2:34am    
any other sugesstions
Member 12355460 14-Mar-16 3:12am    
i found it..
olDDLItem.Items.Add(sdr[0].ToString());
this is working now. thank you
ZurdoDev 1-Apr-16 15:24pm    
Then please post something as a solution so this no longer shows unanswered.

And next time, explain what the problem is.
Karthik_Mahalingam 3-May-16 20:06pm    
Please close this post.

I have used Stored Procedure to get the records from DB.
Use SP instead of writing query in Application.
Below is the code:

C#
public DataTable retrievevalues()
        {
             SqlCommand cmd = new SqlCommand("StoredProcedure", sqlconn);
             cmd.Parameters.AddWithValue("@userid", username);
             cmd.CommandType = CommandType.StoredProcedure;
             sqlconn.Open();
             SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.Default);
             DataTable dt = new DataTable();    
             dt.Load(dr);
             return dt;
        }
---------------
-----------------------------------
C#
ComboBox1.DataSource = retrievevalues();


--------------------------------------------------
 
Share this answer
 
v2
Second Way - By Iteration

C#
public DataTable retrievevalues()
{
SqlCommand cmd = new SqlCommand("StoredProcedure", sqlconn);
cmd.Parameters.AddWithValue("@userid", username);
cmd.CommandType = CommandType.StoredProcedure;
sqlconn.Open();
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.Default);
DataTable dt = new DataTable(); 
dt.Load(dr);
return dt;
}


NOTE: @userid is an Stored Procedure Variable you can ignore that.
-----------------------------------------------------
C#
DataTable DT = new DataTable();
DT = retrievevalues();

            foreach (DataRow row in DT.Rows)
            {
combobox1.items.add(row["SQL ColumnName"].ToString()
            }
)

-----------------------------------------------------
 
Share this answer
 
v2
Comments
CHill60 3-May-16 12:38pm    
Better to just use the DataSource of the comboBox as per solution 3

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