如何将字段绑定到文本框

时间:2012-02-14 04:05:19

标签: c# asp.net sql-server

我需要将字段(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(”&lt;%#Bind(\“{0} \”)%&gt;“,”Computertag“);”这行代码不会将值绑定到文本框。如何将值分配给文本框?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您可以使用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