-
Sep 3rd, 2010, 08:51 PM
#1
Thread Starter
Member
[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
-
Sep 3rd, 2010, 09:55 PM
#2
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:
myDataGridView.DataSource = myDataTable 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.
-
Sep 4th, 2010, 10:53 AM
#3
Thread Starter
Member
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
-
Sep 4th, 2010, 11:10 AM
#4
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
-
Sep 4th, 2010, 11:10 AM
#5
Re: VB.NET populate text box using code
Originally Posted by Darraca
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".
-
Sep 7th, 2010, 03:53 AM
#6
Thread Starter
Member
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
-
Sep 7th, 2010, 07:08 AM
#7
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
-
Sep 7th, 2010, 07:14 AM
#8
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?
-
Sep 7th, 2010, 07:25 AM
#9
Re: VB.NET populate text box using code
Originally Posted by techgnome
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.
-
Sep 7th, 2010, 08:14 AM
#10
Thread Starter
Member
Re: VB.NET populate text box using code
Great! thanks, got this working now.
much appreciated
-
Sep 7th, 2010, 09:59 AM
#11
Thread Starter
Member
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
-
Sep 7th, 2010, 07:53 PM
#12
Re: VB.NET populate text box using code
Originally Posted by Darraca
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.
-
Sep 8th, 2010, 09:49 AM
#13
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|