将列值绑定到TextBox

时间:2013-11-14 03:35:56

标签: c# asp.net

我想将SQL查询返回的值绑定到文本框:

 protected void cmbPujaName_SelectedIndexChanged(object sender, EventArgs e)
    {
        string pujaselectedid = null;

        pujaselectedid = cmbPujaName.DataValueField;

        SqlConnection con2 = null;
        con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["SRKBSDB"].ConnectionString);
        SqlDataAdapter pamt = new SqlDataAdapter("select Amount from PoojaDietyMaster where PoojaDietyMasterID =" + cmbPujaName.DataValueField, con2);
        DataSet pamtds = new DataSet();
        pamt.Fill(pamtds);
        //cmbPujaName.DataSource = pamtds;
        txtAmount.Text = pamtds -- Not sure what to add here
        txtAmount.DataBind();
    }

请帮忙

4 个答案:

答案 0 :(得分:3)

添加此 txtAmount.Text=pamtds.Tables[0].Rows[0]["Amount"].ToString();

答案 1 :(得分:0)

您可以将TextBox设置为DataSource中的一个特定单元格值,如下所示:

txtAmount.Text=pamtds.Tables[0].Rows[0][0].ToString();

答案 2 :(得分:0)

 SqlCommand c = new SqlCommand("select Amount from PoojaDietyMaster where PoojaDietyMasterID =" + cmbPujaName.DataValueField,con2);
 double c1 = (double)c.ExecuteScalar(); 
 txtAmount.Text = c1.ToString();

因为你只是得到一个值。没有必要将它加载到Datatable

答案 3 :(得分:0)

txtAmount.Text=pamtds.Tables[0].Rows[0]["Amount"].ToString();

  make sure the field names in the select result have 'Amount' column in it..!!

删除此代码

 txtAmount.DataBind();
相关问题