Sas
Sas

Reputation: 2503

How to bind field to a text box

I need to bind a field(ComputerTag) to a Text field.

This my code:

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();
    }
}

but "txtBxCompTag.Text = string.Format("<%# Bind(\"{0}\") %>", "Computertag");" this line of code doesn't bind the value to the text box. How can I assign the value to the text box?

Upvotes: 2

Views: 834

Answers (2)

findcaiyzh
findcaiyzh

Reputation: 647

You can use 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]));
        }
    }
}

Above code comes from msdn: http://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.90).aspx

Upvotes: 1

Adeel
Adeel

Reputation: 19238

The function ExecuteNonQuery is used for insertion/updation. Use SqlDataReader

Upvotes: 1

Related Questions