0

I am trying to bind drop-down box from code behind, but I am getting a compilation error:

'System.Web.UI.WebControls.SqlDataSource' does not contain a definition for 'DataSource'

I have tried to figure out but cannot seem to fix this issue.

<asp:DropDownList ID="MYDDL" Width="300px" DataTextField="PRJ_TITLE" AutoPostBack="true"
                  DataValueField="PRJ_ID" runat="server">
</asp:DropDownList> 

Here is my function:

private void Bind_DD()
{
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;
    SqlConnection con2 = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();
    SqlCommand cmd1 = new SqlCommand("SELECT  ID, PRJ_TITLE FROM myTable");

    cmd1.Connection = con2;
    con2.Open();

    myDDL.DataSource = cmd1.ExecuteReader();
    myDDL.DataTextField = "PRJ_TITLE";
    myDDL.DataValueField = "ID";
    myDDL.DataBind();

    con2.Close();

}
7
  • Please post all relevant code. In the current snippet there is no SqlDataSource control mentioned. Commented Feb 3, 2014 at 18:11
  • Why are you databinding twice? Commented Feb 3, 2014 at 18:14
  • Andrei, i don't have sql datasource in the aspx that is why i was trying to bind it in the code behind. I am not sure what am i doing wrong here. thanks Commented Feb 3, 2014 at 18:14
  • Josh, u just updated my code. thanks Commented Feb 3, 2014 at 18:16
  • @moe, what was the code in the first case? And what is the problem with code-behind approach? Commented Feb 3, 2014 at 18:17

2 Answers 2

4

So it looks like you are possibily a bit mixed up about sql data connections as there are many ways to do them. I elected to do a databing via the sqlDataAdapter to DataTable.

Additionally make sure you do not have any elements in your markup that have a asp:SqlDataSource anywhere in your markup.

private void Bind_DD()
{
    DataTable dt = new DataTable();

    using(SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ConnectionString))
    {
        con2.Open();

        SqlCommand cmd1 = new SqlCommand("SELECT  ID, PRJ_TITLE FROM myTable",con2);
        SqlDataAdapter sda = new SqlDataAdapter(cmd1);
        sda.Fill(dt);
    }

    myDDL.DataSource = dt;
    myDDL.DataTextField = "PRJ_TITLE";
    myDDL.DataValueField = "ID";
    myDDL.DataBind();

}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

private void Bind_DD()
{
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;
    SqlConnection con2 = new SqlConnection(strConnString);

    DataSet ds = new DataSet();
    SqlCommand cmd1 = new SqlCommand("SELECT  ID, PRJ_TITLE FROM myTable");
    cmd1.Connection = con2;
    con2.Open();
    SqlDataAdapter sda = new SqlDataAdapter(cmd1);
    sda.Fill(ds);
    myDDL.DataSource = ds; //cmd1.ExecuteReader();
    myDDL.DataTextField = "PRJ_TITLE";
    myDDL.DataValueField = "ID";
    myDDL.DataBind();
    con2.Close();
    //myDDL.DataBind(); 
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.