2012-02-14 37 views
2

我需要将字段(ComputerTag)绑定到文本字段。如何将字段绑定到文本框

此我的代码:

public void load() 
{ 
    //Intializing sql statement 
    string sqlStatement = "SELECT Computertag FROM Computer WHER 
    [email protected]"; 

    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(” <%#绑定(\ “{0} \”)%> “ ”Computertag“);”这行代码不会将该值绑定到文本框。我怎样才能将值分配给文本框?

回答

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

+0

感谢,作为一个新手乌尔xample帮了不少忙。 – Sas 2012-02-14 14:13:44