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?
I think you are calling
Fill1()method in page load. When page is PostBack then in page load eventFill1()again called and your selection will be cleared. you need to this in your load event