通过选择下拉列表,将c#整数数据加载到文本框

时间:2012-06-02 06:19:27

标签: c#

我试图通过选择下拉列表项将整数值从db加载到文本框。我没有收到任何错误,但是文本框中没有显示该值。这是我写的代码,请纠正我,

public void SiteNo()
    {
        Conhr.Open();
        //int anInteger;
        //anInteger = Convert.ToInt32(TextBox1.Text);
        //anInteger = int.Parse(TextBox1.Text);

        string sq = "select SiteCode from tbl_SiteMaster where  Sitealiasname='" + ddlsite.SelectedItem.Text + "' ";
        SqlCommand d = new SqlCommand(sq, Conhr);
        SqlDataReader r;
        r = d.ExecuteReader();
        while (r.Read())
        {
            TextBox1.Text = r.GetValue(0).ToString();
        }
        r.close();
        Conhr.Close();
    }

1 个答案:

答案 0 :(得分:0)

//change code to this to find the problem

public void SiteNo()
{
    Conhr.Open();
    //int anInteger;
    //anInteger = Convert.ToInt32(TextBox1.Text);
    //anInteger = int.Parse(TextBox1.Text);

    string sq = "select count(SiteCode) from tbl_SiteMaster where  Sitealiasname='" + ddlsite.SelectedItem.Text + "' ";
    SqlCommand d = new SqlCommand(sq, Conhr);
    SqlDataReader r;
    r = d.ExecuteReader();
    while (r.Read())
    {
        TextBox1.Text = r.GetValue(0).ToString();
    }
    r.close();
    Conhr.Close();
}