Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 30-Nov-12 22:55pm
Edited 30-Nov-12 23:09pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 Smile | :)
  Permalink  
Comments
sumit kausalye at 1-Dec-12 5:07am
   
It still give error
Error:
Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.DropDownList'.
sumit kausalye at 1-Dec-12 5:14am
   
we can't write tostring() on left hand side, it gives error
Krunal Rohit at 1-Dec-12 5:20am
   
ddl.SelectedItem = dtsponcor.Rows[0]["sponcorid"];
sumit kausalye at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 395
1 Maciej Los 268
2 OriginalGriff 230
3 CHill60 221
4 Sibeesh KV 139


Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 1 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100