Moving SQL data to a textbox RRS feed

  • Question

  • I am trying to get SQL data to appear in a text box.  For example, on table Customers, I have two colums: First_Name and Last_Name.  I want to get that data that into two text boxes. Any thoughts?
    Thursday, November 22, 2007 5:22 AM

All replies

  • There are few simple ways.

    1) You make a connection to the database , open( ) the connection

    2)fire a querry that will get the value either in a Dataset or in a ouput variable.(depends on what technique you opt).

    3)set the value of the textbox like this  TextBox1.value=ds.Tables[0].Rows[0]["ColumnName"].ToString();

    4) i think u can also bind the single value to the textbox.




    Thursday, November 22, 2007 6:31 AM
  • Will this work?
    Code Block

    Dim queryString As String = _
            "Select (@name1, @name2, @name3)" & _
            "FROM dbo.Winning_Num"

            Using conDB1
                Dim command As New SqlClient.SqlCommand(queryString, conDB1)

                Dim reader As SqlClient.SqlDataReader = command.ExecuteReader()

                 While reader.Read()
                    txtGivenName1.Text = "@name1"
                    txtGivenName2.Text = "@name2"
                    txtGivenName3.Text = "@name3"
                End While


            End Using

    Friday, November 23, 2007 6:56 AM


    I think this will work fine only if u have one row/record.

    if your sql returns multiple records you will see only the last row data in your textboxes.



    Friday, November 23, 2007 7:09 AM
  • Hi,

    The best practice is to keep the UI and the database code separate.  Here are some instructions on how to create a basic Windows Forms application that connects to a database.


    First, create a new project and add a database to the project (Project/Add new item/Database (.mdf)). Double click on that database in the solution explorer to open the database explorer / server explorer. Expand the nodes of the db tree to find the Tables node and right-click on it and select New Table. Add the fields that you require. Save the table (I’ll assume you call it Foo).


    Select the database in the solution explorer and go to the property window. Ensure that the Copy to Output Directory setting is set to Copy if newer.


    Next you need to create a dataset. Project/Add new item/Dataset. I'll assume you call it AppData. The dataset will become the data layer of your application.


    In the dataset designer, drag all your tables from the server explorer. The server explorer is accessed from View/Server Explorer. You can add a new data connection to your database (sql server, access, odbc...) if it’s not already there. Save and compile.


    Create a new form. Open the Data Source pane (data/show data source) and you will see your dataset as a datasource. I'll assume you'll create a form for table Foo. Next you need to decide if you want a grid view or a detail view. Click on the Foo table in the data source window and you will see a dropdown appear. If you want a detail view instead of a grid change it with this dropdown.


    Now drag the table to the empty form. You will see that a grid (or text boxes and labels) will be created with each of the fields. A BindingSource object, a binding navigator, a table adapter and an instance of your dataset are also created. Now switch to code view. You will notice in the Form_Load event some code to load the data and on the save toolstrip code to end editing and validate the data.


    Let's suppose you also want to save back the changes to the database. Add this line of code:



    That's it. Now you have an application that can insert, delete, update and browse all the data in table Foo.


    A whole bunch of good resources, including numerous books, that could really help you out:


    ASP.NET 2.0:

    Winforms: and

    The online MSDN Developer's Guide for Windows Form Applications is also indispensible:


    They will all teach you the basics of databinding and data access.



    Saturday, December 1, 2007 12:43 AM
  • any one can giv VB codeing plzzzzzz urgent
    Sunday, December 30, 2007 6:13 AM