2012-02-14 2 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 (" <% # Bind (\ "{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

1

Функция ExecuteNonQuery используется для вставки/обновления. Используйте SqlDataReader

Смежные вопросы