Click here to Skip to main content
12,630,420 members (30,723 online)
Rate this:
 
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 23:55pm
Updated 1-Dec-12 0:09am
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 :)
  Permalink  
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
Krunal Rohit 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 1 Dec 2012
Copyright © CodeProject, 1999-2016
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