如何将字段绑定到文本框

本文关键字:文本 绑定 字段 | 更新日期: 2023-09-27 18:24:18

我需要将字段(ComputerTag)绑定到Text字段。

这是我的代码:

public void load() 
{
    //Intializing sql statement
    string sqlStatement = "SELECT Computertag FROM Computer WHER 
    ComputerID=@ComputerID";
    SqlCommand comm = new SqlCommand();
    comm.CommandText = sqlStatement;
    int computerID = int.Parse(Request.QueryString["ComputerID"]);
    //get database connection from Ideal_dataAccess class
    SqlConnection connection = Ideal_DataAccess.getConnection();
    comm.Connection = connection;
    comm.Parameters.AddWithValue("@ComputerID", computerID);
    try
    {
       connection.Open();
       comm.ExecuteNonQuery();
       //Bind the computer tag value to the txtBxCompTag Text box
       txtBxCompTag.Text= string.Format("<%# Bind('"{0}'") %>", "Computertag");
    }
    catch (Exception ex)
    {
        Utilities.LogError(ex);
        throw ex;
    }
    finally
    {
        connection.Close();
    }
}

但是"txtBxCompTag.Text=string.Format("<%#Bind(''"{0}''")%>","Computertag");"这行代码没有将值绑定到文本框。如何将值分配给文本框?

如何将字段绑定到文本框

您可以使用ExecuteReader

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}", reader[0]));
        }
    }
}

以上代码来自msdn:http://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.90).aspx

函数ExecuteNonQuery用于插入/更新。使用SqlDataReader