【问题标题】:Get data from sql query into textbox从sql查询中获取数据到文本框
【发布时间】:2012-03-18 19:31:56
【问题描述】:

通过在页面初始化关闭连接然后在下拉列表重新打开来修复它。

我已经搜索过这个问题的答案,但没有任何运气。

我想从选定的下拉列表项中获取数据到文本框。我一直在尝试执行这个 sql 查询,但没有成功。

这是我的代码:

    public partial class EditContact : System.Web.UI.Page
{
    SqlConnection connection = new SqlConnection("SqlConnection");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_Init(object sender, EventArgs e)
    {
        connection.Open();
        SqlCommand SqlCommandDD = new SqlCommand("SELECT FirstName + ' ' + LastName AS 'TextField', Contact_ID, Email, PhoneNumber, CompanyID FROM ContactPerson");
        SqlCommandDD.Connection = connection;

        DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
        DropDownList2.DataValueField = "Contact_ID";
        DropDownList2.DataTextField = "TextField";
        DropDownList2.DataBind();

    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string fNameTemp = DropDownList2.SelectedValue;


        string sqlquery = ("SELECT FirstName FROM ContactPerson WHERE (Contact_ID = " + fNameTemp + ")");

        SqlCommand command = new SqlCommand(sqlquery, connection);

        SqlDataReader sdr = command.ExecuteReader();

        fNameTextBox.Text = sdr.ToString();

    }


}

【问题讨论】:

    标签: c# asp.net sql textbox


    【解决方案1】:

    尝试这样修改代码:

    string sqlquery = "SELECT FirstName FROM ContactPerson WHERE Contact_ID = " + fNameTemp;
    
    SqlCommand command = new SqlCommand(sqlquery, connection);
    
    SqlDataReader sdr = command.ExecuteReader();
    
    while ( sdr.Read() )
    {
        fNameTextBox.Text = sdr[ "FirstName" ].ToString();
    }
    

    【讨论】:

      【解决方案2】:

      “ExecuteReader”返回复杂/非标量值。请改用“ExecuteScalar”方法:

      SqlCommand command = new SqlCommand(sqlquery, connection);
      fNameTextBox.Text = command.ExecuteScalar().ToString();
      

      【讨论】:

      • 我收到一条错误消息,提示我已经打开了一个与命令关联的数据读取器。这是什么意思?
      • OK 使用此代码修复它,并在页面初始化时关闭连接,然后重新打开。谢谢!
      【解决方案3】:

      如果我们要从表中获取单个值..那么我们可以使用执行标量而不是数据适配器或数据读取器..

      方法一:

       fNameTextBox.Text = command.ExecuteScalar().ToString();
      

      方法二:(如果你使用任何常用功能)

      object scalarobject;
         scalarobject = command.ExecuteScalar();
      fNameTextBox.Text = scalarobject.ToString();
      

      【讨论】:

        【解决方案4】:

        有几种方法可以实现这一点,通过硬编码关闭连接不是最好的做法,而不是使用 connection.close() 你可以将连接放在使用标签内,这是你的代码使用标签

        using (SqlConnection connection = new SqlConnection("SqlConnection"))
        {
                connection.Open();
                SqlCommand SqlCommandDD = new SqlCommand("SELECT FirstName + ' ' + LastName AS 'TextField', Contact_ID, Email, PhoneNumber, CompanyID FROM ContactPerson");
                SqlCommandDD.Connection = connection;
        
                DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
                DropDownList2.DataValueField = "Contact_ID";
                DropDownList2.DataTextField = "TextField";
                DropDownList2.DataBind();
        }
        

        无需手动关闭连接

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-07
          • 1970-01-01
          • 1970-01-01
          • 2011-07-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多