Click here to Skip to main content
11,718,289 members (83,229 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# SQL-Server-2008
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?
Posted 11-Nov-12 7:18am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

  Print Answers RSS
0 OriginalGriff 215
1 F-ES Sitecore 210
2 Maciej Los 140
3 Jochen Arndt 120
4 Andy Lanng 115
0 OriginalGriff 535
1 Maciej Los 529
2 Richard MacCutchan 440
3 CHill60 415
4 F-ES Sitecore 415

Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100