Reputation: 708
I have a ASP:DropDown list which is populated by the following:
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();
}
}
I need to call the SalesPrice
from same table and based on the selected value in the drop down update a ASP:TextBox
Field called txtUnitAmount
.
I have tried writing it into the same function and it wont let me set the .Text
value for the ASP:TextBox
. Can any one suggest a way to achieve this?
Thanks to all for their help so far!
Cheers
Justin
Extra Code for Discussion
public void Fill2()
{
string getProdValue = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
using (SqlConnection _con = new SqlConnection(getProdValue))
using (SqlCommand cmd = new SqlCommand("SELECT SalesPrice FROM Products WHERE Description = " + Request.Form["txtProduct"].ToString(), _con))
{
int getProdCost = (int)cmd.ExecuteScalar();
txtUnitAmount.Text = getProdCost.ToString("###,###0.000");
}
}
Upvotes: 0
Views: 3246
Reputation: 4092
The question is a little hard to follow, but I will give it a stab.
You going to have to do another query to the DB based on the onChnge event produced by the drop down change.
You need to wire up the OnChange event for the dropdown and enable autopostback. This can be done through the properties explorer. From the OnChangeEvent, fire your code
Note this code is far from production. You need to do input and return sanitization.
var description = Request.Form["txtProduct"].ToString();
using (SqlConnection _con = new SqlConnection(getProdValue))
using (SqlCommand cmd = new SqlCommand("SELECT UnitPrice FROM Products WHERE Description = '" + description "'", _con))
{
cmd.Connection.Open();
var unitPrice = cmd.ExecuteScalar();
txtUnitPrice.Text= unitPrice.ToString("###,###0.000");
}
Upvotes: 1