【问题标题】:how to bind the two values into text box for selection of dropdownlist item如何将两个值绑定到文本框中以选择下拉列表项
【发布时间】:2016-08-30 09:13:32
【问题描述】:

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

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);
    }            
}  

【问题讨论】:

  • 这个System.Data.SqlDbType.Int应该是System.Data.SqlDbType.Varchar
  • 谢谢你.....它的工作,我如何才能将另一个文本值绑定到文本框中,如上述代码中的价格,请帮助我

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


【解决方案1】:
param0.ParameterName = "@title";
param0.SqlDbType = System.Data.SqlDbType.Int;
param0.Value = title;

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

【讨论】:

    【解决方案2】:

    排队

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

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

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

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

    根据PRODUCT NAME的类型列类型。

    【讨论】:

      【解决方案3】:

      替换

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        相关资源
        最近更新 更多