Click here to Skip to main content
15,894,720 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 box.in fact to show cells or columns information or rows information in a text box or in severl text box.
what is the way?
Posted

1 solution

Hi,
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:

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

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

                // 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
                cmd.Parameters.Add(
                    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
                lbFound.Items.Clear();
                while(rdr.Read())
                {
                    yourTextBox.Text+= rdr["FirstName"].ToString() +
                    " " + rdr["LastName"].ToString();
                }
            }
            catch(Exception ex)
            {
                // Print error message
                MessageBox.Show(ex.Message);
            }
            finally
            {
                // Close data reader object and database connection
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }


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 :

http://www.akadia.com/services/dotnet_data_reader.html[^]

I hope it helps,
Cheers
 
Share this answer
 

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