0

I have a datatable being populated however one of the fields is a drop down list which is derived from a database connection. When I click add it always add the same data despite my selection and then removes the first item in the list.

ASPX Code

<asp:TableCell><asp:DropDownList runat="server" ID="txtProduct"></asp:DropDownList></asp:TableCell>

CS Code for Dropdown List

public void Fill1()
{
    string connectionString = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
    using (SqlConnection _con = new SqlConnection(connectionString))
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Products", _con))
    {
        cmd.Connection.Open();

        SqlDataReader ddlValues;
        ddlValues = cmd.ExecuteReader();

        txtProduct.DataSource = ddlValues;
        txtProduct.DataValueField = "Description";
        txtProduct.DataTextField = "Description";
        txtProduct.DataBind();

        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }
}

Any thoughts as to how to fix this?

6
  • A sidenote: you are naming a DropDownList txtProduct? I would assume that a control with this ID is a Textbox. To your question: are you binding the DropDownList only if !Page.IsPostBack? Commented Mar 30, 2011 at 16:10
  • I think you'll find that the call to DataBind resets your selection to the first item. Commented Mar 30, 2011 at 16:19
  • Also, there's no reason to call cmd.Connection.Close and cmd.Connection.Dispose, since you have the command and the connection in using blocks. They'll be disposed automatically. Commented Mar 30, 2011 at 16:20
  • I having a hard time understanding what you mean by "When I click add it always add the same data despite my selection and then removes the first item in the list." Can you please expand on this some more. Commented Mar 30, 2011 at 16:21
  • You should wrap the SqlDataReader in a using clause. Commented Mar 30, 2011 at 16:48

2 Answers 2

1

I think you are calling Fill1() method in page load. When page is PostBack then in page load event Fill1() again called and your selection will be cleared. you need to this in your load event

if(!Page.IsPostBack)
{
   Fill1();
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are missing the WHERE part of your SQL statement and a way to connect the DropDownList selection to the SQL Statment. Something like...

new SqlCommand("SELECT * FROM Products WHERE ProductID = " + txtProduct.SelectedValue , _con))

FYI: This is for illustration only, I would never put two steps in one line of code, I would use parameters and I would add a boat load of error checking.

1 Comment

I have no Id to do this with i Have a ProductCode would that do the same job?

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.