0

Hi i am trying to get DropDownList to work with SqlDataReader but its not populating the DropDownlist. The TextBox.Text Reader is working though.

Here is the code I am using:

    using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
    {
        SqlCommand command =
        new SqlCommand("SELECT * FROM [datarep].[dbo].[OrderHeader] WHERE [OrderNumber] = '"+OrderNumber+"'", con);
        con.Open();

        SqlDataReader read = command.ExecuteReader();

        while (read.Read())
        {
            TextBox2.Text = (read["CreatedDate"].ToString());
            TextBox3.Text = (read["CreatedBy"].ToString());
            CompanyStored = (read["CustomerID"].ToString());
            TextBox7.Text = (read["Store_Number"].ToString());

            DropDownList1.DataTextField = (read["Year"].ToString());
            DropDownList1.DataBind();



        }
        read.Close();
    }
4
  • how many items are being added to the DropDownList..? also move your DropDownList1.DataBind();` out side of the While loop and lookup how to assign DataSource to the DropdownList Commented Dec 3, 2014 at 21:07
  • Just 1 item is being added @DJKRAZE Commented Dec 3, 2014 at 21:07
  • well if there happens to be more I would still move it also look at this MDSN Binding Data to DropDownList Commented Dec 3, 2014 at 21:08
  • Since you're expecting only a single record back, you should remove the while - that makes it look like you could potentially have more than one record, which gets confusing to look at. If you know for sure you will get one record, just do read.Read();. If you're not sure, you can do if (read.Read()). Either way makes it clear that you're only filling in the controls once. Commented Dec 3, 2014 at 21:30

2 Answers 2

1

Assuming your dropdown is already populated with the list of years, you need to set its value rather than setting its DataTextField - that property is meant for defining the column or property name of the data source for text, not setting the selected value itself.

DropDownList1.SelectedValue = read["Year"].ToString();

If you don't have the dropdown even populated yet, then you have to populate it in advance, using a data source, which is probably a list of years. For example, suppose you want all years between 2000 and 2050, something like this might do the trick:

var dataSource = Enumerable.Range(2000, 51)
   .Select(x => new { TheYear = x })
   .ToArray();

DropDownList1.DataSource = dataSource;
DropDownList1.DataValueField = "TheYear";
DropDownList1.DataTextField = "TheYear";
DropDownList1.DataBind();

Note that the DataTextField and DataValue field represent the property of the object in the data source.

For something simple like numbers, you can populate the dropdown one at a time as well, rather than using a data source - it's the same result in the end.

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

Comments

0

SqlDataReader reads your data line by line. If you want to use a DataReader you'll have to add a new list item to your DDL per iteration

Try it like this:

while (read.Read())
{ 
    /*code omitted*/
    var item = new ListItem(); 
    item.Text = read["Year"].ToString();
    item.Value = read["Year"].ToString();
    DropDownList1.Items.Add(item);
}

Further reading and alternative solutions:

4 Comments

I get this error DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name '2013' @Serv
DropDownList1.DataBind();
try setting DropDownList1.DataValueField= "Year" DropDownList1.DataTextField = "Year"; I am not sure of the syntax right now.
thank you, it works now! had to add DropDownList1.Items.Clear(); to clear it from adding it multiple times

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.