Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void BindGridview()
{
con.Open();

string sponcor = "SELECT sponcorid FROM Registration_Master";
SqlCommand com = new SqlCommand(sponcor, con);
com.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dtsponcor = new DataTable();
da.Fill(dtsponcor);

DropDownList ddl = new DropDownList();

ddl = dtsponcor.Rows[0]["sponcorid"];

con.Close();
ddl.DataSource = dtsponcor;
ddl.DataTextField = "sponcorid";
ddl.DataValueField = "sponcorid";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));

}
//Error :
//Error 4 Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.DropDownList'. //An explicit conversion exists (are you missing a cast?)

//what is the solution for that??
Posted
Updated 30-Nov-12 23:09pm
v3

1 solution

Go with this code:
protected void BindGridview()
    {
         con.Open();
 
                string sponcor = "SELECT sponcorid FROM Registration_Master";
               SqlCommand com = new SqlCommand(sponcor, con);
                com.CommandType = CommandType.Text;
               SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dtsponcor = new DataTable();
                da.Fill(dtsponcor);
 
                DropDownList ddl = new DropDownList();
 
                ddl = dtsponcor.Rows[0]["sponcorid"];

            
             con.Close();
            ddl.DataSource = dtsponcor;
            ddl.DataTextField = "sponcorid";
            ddl.DataValueField = "sponcorid";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));
 
    }

You're getting that error, because you were String to DropdownList... And that is not possible.. Okay, so here I have solved your problem :)
 
Share this answer
 
Comments
sumit kausalye 1-Dec-12 5:07am    
It still give error
Error:
Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.DropDownList'.
sumit kausalye 1-Dec-12 5:14am    
we can't write tostring() on left hand side, it gives error
[no name] 1-Dec-12 5:20am    
ddl.SelectedItem = dtsponcor.Rows[0]["sponcorid"];
sumit kausalye 1-Dec-12 8:00am    
no same error

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