Results 1 to 13 of 13

Thread: [RESOLVED] VB.NET populate text box using code

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Resolved [RESOLVED] VB.NET populate text box using code

    Hi,

    I am using VB.NET 2008 and I have made a connection to an SQL Server database using code. I have managed to populate a datagridview control with the contents of a selected table using a query that I passed in. What I want to do now is set a text box to display a selected field from the query using code.

    Can anyone tell me how to do this? I used to do this using an adodb.recordset in vb6 but I am new to VB.NET

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: VB.NET populate text box using code

    You don't need to use code. Unless you need something that it can't handle, you should use data-binding. You're already data-binding the DataTable to the DataGridView, so you should just bind it to the TextBox(es) as well:
    vb.net Code:
    1. myDataGridView.DataSource = myDataTable
    2. myTextBox.DataBindings.Add("Text", myDataTable, "MyColumn")
    Now, each time you select a row in the grid, the TextBox will be updated with the field value from that row. Any edits you make in the TextBox will be automatically pushed to the underlying DataTable and, as such, the grid too.

    If you've bound your grid in the designer then you can bind your TextBox in the designer too, using its (DataBindings) property.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Re: VB.NET populate text box using code

    Hi jmcilhinney,

    I didn't use a data table in my code, here's how I got the datagridview control to populate:

    Code:
    Dim conn As SqlConnection
            Dim cmd As New SqlCommand
            Dim da As New SqlDataAdapter
            Dim ds As New DataSet
            Dim SQL As String
    
            conn = New SqlConnection(m_sConnectionString)
            SQL = "SELECT * FROM tblUsers"
    
            Try
                cmd = conn.CreateCommand
                cmd.CommandText = SQL
                da.SelectCommand = cmd
                da.Fill(ds, "tblUsers")
    
                DataGridView1.DataSource = SQLds
                DataGridView1.DataMember = "tblUsers"
                DataGridView1.ReadOnly = True
    
            Catch ex As Exception
                MessageBox.Show("An error has occurred: " & ex.Message.ToString())
            End Try
    I want to get a value from a column called 'Username' from a table called tblUsers. I would like to be able to loop through each record until I match a name and then display that name in a text box.

    Thanks

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: VB.NET populate text box using code

    What do you think a DataSet is? It is a collection of DATATABLES! So even though you didn't explicitly create a datatable variable, you DID create one inside of the dataset implicitly.

    So you do have a datatable. That means you can loop through it for each datarow in the datatable... and check the UserName field and see if it matches.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: VB.NET populate text box using code

    Quote Originally Posted by Darraca View Post
    I didn't use a data table in my code
    Yeah, you did. This:
    Code:
    da.Fill(ds, "tblUsers")
    is populating a DataTable named "tblUsers".

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Re: VB.NET populate text box using code

    Perfect! Thanks very much! Can I use this method to populate a combobox? I tried ComboBox1.DataBindings.Add("Text", ds, "Table1.Description") but this only added the first item.

    Cheers

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: VB.NET populate text box using code

    Don't use that form of binding... set the datasource (to the dataset) and datamember (to the NAME of the datatable)... the DisplayMember to name of the field to DISPLAY and then the ValueMember to the name of the field that you want to use as the value.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: VB.NET populate text box using code

    I showed how to data-bind a DataGridView, which can display multiple items, and a TextBox, which can display one item. Which is a ComboBox more like?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: VB.NET populate text box using code

    Quote Originally Posted by techgnome View Post
    Don't use that form of binding... set the datasource (to the dataset) and datamember (to the NAME of the datatable)
    A ComboBox doesn't have a DataMember. You have to assign the DataTable itself to the DataSource.

    Any time you use the DataBindings property you are performing simple data-binding, while setting the DataSource is complex data-binding. All controls support simple data-binding but displaying all items in a list all at the same time is always going to be complex data-binding.

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Smile Re: VB.NET populate text box using code

    Great! thanks, got this working now.
    much appreciated

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Re: VB.NET populate text box using code

    Just one last thing, what is the best way to assign a value from a database straight to a variable?

    Cheers

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: VB.NET populate text box using code

    Quote Originally Posted by Darraca View Post
    Just one last thing, what is the best way to assign a value from a database straight to a variable?

    Cheers
    To get data from a database you would use ADO.NET to execute a query. You can call Update on a DataAdapter to populate a DataTable, call ExecuteReader on a Command to create a DataReader or call ExecuteScalar on a Command to get a single value. You can then get the data from that DataTable or DataReader and do whatever you want with it, or do whatever you want with that single value. Follow the Database FAQ link in my signature and you'll find resources that show you how to do all three.

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Talking Re: VB.NET populate text box using code

    Great! thanks very much, i used the FAQ link and found loads of useful tips.
    Thanks again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width