Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have written the following code to fill the dropdownlist 'ddlstuid'.
The following code gives me an error: "The best overloaded method match for ddlstuid has some invalid arguments". Pls tell me how else can i fill a dropdownlist in asp.net using c# code.

C#
SqlConnection myconn;
SqlCommand cmd;
string str = "Data Source=TIMSCDR\\SQLEXPRESS;Initial Catalog=IAP;Integrated Security=True";
SqlDataReader dr;

     try
    {
        cmd = new SqlCommand("select stuid from attendance", myconn);
        myconn.Open();
        ddlstuid.Items.Clear();
        dr = cmd.ExecuteReader();
        if (dr != null)
        {
            while (dr.Read())
            {
                ddlstuid.Items.Add(dr["stuid"]);
            }
        }
        myconn.Close();
    }
    catch (Exception e1)
    {
        Label45.Text = "Error ocurred" + e1;
    }
Posted
Comments
cuteband 6-Jun-11 2:03am    
ddlstuid.Items.Add(dr["stuid"].tostring());
Convert this into the String
debo_india 6-Jun-11 2:04am    
thanx now no error is coming!!thanx a lot!! :)
cuteband 6-Jun-11 2:06am    
please vote on me

C#
ddlstuid.Items.Add(dr["stuid"].tostring());
 Convert this into the String
 
Share this answer
 
There are a number of ways to do this:
1) You could use a DataAdapter and DataTable:
C#
SqlDataAdapter da = new SqlDataAdapter("SELECT stuid FROM attendance", myconn);
DataTable dt = new DataTable();
da.Fill(dt);
ddlstuid.Items.Clear();
ddlstuid.DataSource = dt;

2) You could construct a list and use that:
C#
try
    {
    cmd = new SqlCommand("select stuid from attendance", myconn);
    myconn.Open();
    ddlstuid.Items.Clear();
    dr = cmd.ExecuteReader();
    List<object> list = new List<object>();
    if (dr != null)
        {
        while (dr.Read())
            {
            list.Add(dr["stuid"]);
            }
        }
    myconn.Close();
    ddlstuid.DataSource = list;
    }
catch (Exception e1)
    {
    Label45.Text = "Error ocurred" + e1;
    }
Personally, I prefer the former...
 
Share this answer
 
Comments
debo_india 6-Jun-11 2:21am    
thanx i will try it!
you need to convert dr["stuid"]) into string as:


dr["stuid"]).toString()
 
Share this answer
 
Comments
debo_india 6-Jun-11 2:21am    
thanx i converted it just now into string. the error is not coming anymore :)

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