如何将两个值绑定到文本框中以选择下拉列表项

时间:2016-08-30 09:13:33

标签: c# sql asp.net visual-studio

我正在尝试发票项目,当我选择下拉列表项目时,我应该从数据库中获取价格和折扣并显示在文本框中,然后获取价格值到文本框但是我怎样才能得到另一个值是折扣到文本框如何绑定第二个文本框值...请一些身体帮助我....谢谢你

protected void Page_Load(object sender, EventArgs e)
{
    if (! IsPostBack )
    {
        populatedropproduct();
    }
}

private void populatedropproduct()
{
    SqlCommand com = new SqlCommand("SELECT * FROM invoice1", conn);
    com.Connection.Open();
    SqlDataReader rdr = com.ExecuteReader();
    dropproduct.DataSource = null;
    dropproduct.DataTextField = "PRODUCT_NAME";
    dropproduct.DataValueField ="PRICE";
   dropproduct.DataBind();
    dropproduct.Items.Insert(0, new ListItem("--Select Item--", "0"));
    com.Connection.Close();
}
protected void txtinvoice_TextChanged(object sender, EventArgs e)
{

}
protected void txtcustomer_TextChanged(object sender, EventArgs e)
{

}
protected void dropproduct_SelectedIndexChanged(object sender, EventArgs e)
{
    String title = dropproduct.SelectedItem.Value;
    title = dropproduct.SelectedItem.Value;
    try
    {
        SqlCommand com = new SqlCommand("SELECT PRICE,[DISCOUNT%]  FROM                  invoice1 WHERE [PRODUCT NAME]=@title", conn);
        SqlParameter param0 = new SqlParameter();
        param0.ParameterName = "@title";
        param0.SqlDbType = System.Data.SqlDbType.NVarChar;
        param0.Value = title;
        com.Parameters.Add(param0);
        com.Connection.Open();
        SqlDataReader rdr = com.ExecuteReader();
       txtprice.Text = title.ToString();
        com.Connection.Close();
        com.Connection.Dispose();
    }
    catch(Exception s)
    {
       HttpContext.Current.Response.Write("Error Occured " + s.Message);
    }            
}  

3 个答案:

答案 0 :(得分:0)

param0.ParameterName = "@title";
param0.SqlDbType = System.Data.SqlDbType.Int;
param0.Value = title;

您的代码说param @title是整数,但您尝试将其放入字符串中。

答案 1 :(得分:0)

在第

param0.SqlDbType = System.Data.SqlDbType.Int;

您将类型或参数设置为Int。但是你的PRODUCT NAME肯定是一个字符串。所以将该行更改为

param0.SqlDbType = System.Data.SqlDbType.VarChar;

param0.SqlDbType = System.Data.SqlDbType.NVarChar;

根据PRODUCT NAME列类型的类型。

答案 2 :(得分:0)

替换

param0.SqlDbType = System.Data.SqlDbType.Int;

使用

param0.SqlDbType = System.Data.SqlDbType.Varchar;