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?
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?DataBindresets your selection to the first item.cmd.Connection.Closeandcmd.Connection.Dispose, since you have the command and the connection inusingblocks. They'll be disposed automatically.