Click here to Skip to main content
15,171,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,
I want to show sql command answer in text fact to show cells or columns information or rows information in a text box or in severl text box.
what is the way?

1 solution

As I realized you want to show the result in a TextBox instead of a GridView or other controls. Well, you can use SqlDataReader along with SqlCommand to fetch the data and then put it in the TextBox. Here is a sample code:

SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;

                // Open connection to the database
                string ConnectionString = "server=xeon;uid=sa;"+
                    "pwd=manager; database=northwind";
                con = new SqlConnection(ConnectionString);

                // Set up a command with the given query and associate
                // this with the current connection.
                string CommandText = "SELECT FirstName, LastName" +
                                     "  FROM Employees" +
                                     " WHERE (LastName LIKE @Find)";
                cmd = new SqlCommand(CommandText);
                cmd.Connection = con;

                // Add LastName to the above defined paramter @Find
                    new SqlParameter(
                    "@Find", // The name of the parameter to map
                    System.Data.SqlDbType.NVarChar, // SqlDbType values
                    20, // The width of the parameter
                    "LastName"));  // The name of the source column

                // Fill the parameter with the value retrieved
                // from the text field
                cmd.Parameters["@Find"].Value = txtFind.Text;

                // Execute the query
                rdr = cmd.ExecuteReader();

                // Fill the list box with the values retrieved
                    yourTextBox.Text+= rdr["FirstName"].ToString() +
                    " " + rdr["LastName"].ToString();
            catch(Exception ex)
                // Print error message
                // Close data reader object and database connection
                if (rdr != null)

                if (con.State == ConnectionState.Open)

The idea is to use regular Ado.Net commands to get the data normally and then put it in a TextBox. Here is the full text of the above sample :[^]

I hope it helps,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900