Click here to Skip to main content
Rate this: bad
good
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 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 11-Nov-12 7:18am

1 solution

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

Solution 1

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:
 
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2014
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