Dataset and Textboxes

xero

Member
Joined
Nov 8, 2004
Messages
12
Programming Experience
1-3
Hi i am a newbie when it comes down to .net programming. Especially VB.net. I am fluent in VBScript and ASP but not VB and VB.net so please bare with me. My current problem is I would like to do the simple thing known as assigning a textbox the value thats in the database. In vbscript it would be something like this...

tbx1.text = rsName("FieldName")

But in VB.net i have no idea... Can someone please help.. This is absolutely driving me insane.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
textbox1.text = bindingmanager("text","Table","Database") is roughly what you want, i dont know the exact command & arguements but it's close to that, i can post more on that tonight.
 

xero

Member
Joined
Nov 8, 2004
Messages
12
Programming Experience
1-3
I do not believe that syntax is doable.....

Also i am curious about one thing... If i where to link the textboxes to the dataset in the formload.... Then that would automatically make the textboxes whatever the dataset consisted of?

And if that is true how would i jump to a specific record?

For example if i wanted to look up record a record carying the number 123 how would i do this???? THanks again everyone!
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
The correct syntax to bind a control to a field in a dataset is:

ControlName.DataBindings.Add(New System.Windows.Forms.Binding(propertyName, dataSource, dataMember))

To look up a record, you could filter a dataView or use a SQL statement to retrieve only the record you want. As always with .NET there are many solutions.

I would suggest reading a little about ADO.NET. The MSDN site is always a good place to start (Here's a link) and there are many other tutorials on the net.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
i was close enough for only haveing my limited memory on hand:

Textbox1.DataBindings.Add("text", DataSource, "Field")
 

xero

Member
Joined
Nov 8, 2004
Messages
12
Programming Experience
1-3
Thats dandy, but im still having problems...

Im starting to get pissed off too because this would only take two lines of freaking code to get this done in plain ADO.. but with ADO.net it's a freaking headache... Anyways i know it's still just a learning process i need to get through... Heres what i am doing....

Private Sub Btn_Lookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_lookup.Click
Dim daStatus2 As New SqlDataAdapter("Select * from tbl_Status where Workorder=" & tbx_WorkOrder.Text & " order by StatusDate ASC", conSQLStatusCheck)
Dim dsStatusCheck As DataSet = New DataSet("tbl_status")
daStatus2.Fill(dsStatusCheck)
tbx_COM.DataBindings.Add("TEXT", daStatus2, "status")
tbx_Company.DataBindings.Add("TEXT", daStatus2, "Company")
tbx_CompanyRep.DataBindings.Add("TEXT", daStatus2, "CompanyRep")
tbx_Finish.DataBindings.Add("TEXT", daStatus2, "Finish")
End Sub


When i click on a button it's suppose to basically look up the info based on what the number is in the textbox yet i keep getting this error:

An unhandled exception of type 'System.ArgumentException' ocurred in systemWindows.Forms.dll

Additional Infomration: Can't Bind to property or column status on DataSource.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
The second parameter in this line:
tbx_COM.DataBindings.Add("TEXT", daStatus2, "status")
should be the DataSource. It should read:
tbx_COM.DataBindings.Add("TEXT", dsStatusCheck.Tables(0), "status")

This won't really work for multiple searches. The second time you click the button, you'll get a message stating that there would be multiple dataBindings when you try to add the binding again (if you make sure to open the connection before trying to fill the dataAdapter, close it after you're done too).

If you are looking to do multiple searches, the best thing would be to look at using a dataView and typed dataSets.
 

xero

Member
Joined
Nov 8, 2004
Messages
12
Programming Experience
1-3
Then how exactly would i write it so I could post do a search multiple times?... That's what i was originally intending. Is there anyway you can write me up a small example. Thanks!
 

ilchophe

New member
Joined
Nov 30, 2004
Messages
2
Programming Experience
1-3
Data Binding

try the following :

Private Sub Btn_Lookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_lookup.Click
Dim daStatus2 As New SqlDataAdapter("Select * from tbl_Status where Workorder=" & tbx_WorkOrder.Text & " order by StatusDate ASC", conSQLStatusCheck)
Dim dsStatusCheck As DataSet = New DataSet("tbl_status")
daStatus2.Fill(dsStatusCheck)
tbx_COM.DataBindings.Add("TEXT", daStatus2, daStatus2.tables(0).TableName & "status")
tbx_Company.DataBindings.Add("TEXT", daStatus2,daStatus2.tables(0).TableName & "Company")
tbx_CompanyRep.DataBindings.Add("TEXT", daStatus2,daStatus2.tables(0).TableName & "CompanyRep")
tbx_Finish.DataBindings.Add("TEXT", daStatus2, "Finish")
End Sub
 

ilchophe

New member
Joined
Nov 30, 2004
Messages
2
Programming Experience
1-3
Hi xero

sorry this is what I meant . . .

Private Sub Btn_Lookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_lookup.Click
Dim daStatus2 As New SqlDataAdapter("Select * from tbl_Status where Workorder=" & tbx_WorkOrder.Text & " order by StatusDate ASC", conSQLStatusCheck)
Dim dsStatusCheck As DataSet = New DataSet("tbl_status")
daStatus2.Fill(dsStatusCheck)
tbx_COM.DataBindings.Add("TEXT", daStatus2, daStatus2.tables(0).TableName & ".status")
tbx_Company.DataBindings.Add("TEXT", daStatus2,daStatus2.tables(0).TableName & ".Company")
tbx_CompanyRep.DataBindings.Add("TEXT", daStatus2,daStatus2.tables(0).TableName & ".CompanyRep")
tbx_Finish.DataBindings.Add("TEXT", daStatus2,daStatus2.tables(0).TableName & ".Finish")
End Sub

see if that works for u
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
As I stated before, The second parameter in this line:
tbx_COM.DataBindings.Add("TEXT", daStatus2, "status")
should be the DataSource. It should read:
tbx_COM.DataBindings.Add("TEXT", dsStatusCheck.Tables(0), "status")
instead of the dataAdapter (daStatus2.tables(0).TableName). DataAdapters don't have Tables and that's why the error message "Tables is not a member of System.Data.SQLClient.SQLDataAdapter" is received.
Also, when performing the search a second time, you would receive an error that multiple bindings aren't permitted since the textBox already has a dataBinding. Of course you could remove the dataBinding (clear) and then add it back, but then there is no sense in using dataBindings at all. It would be best to simply use a dataReader and set the text property of the textBox. Here's a link to a sample app using this idea: DBSearchQuery.zip

Since multiple searches are to be done, I would suggest reading the entire Table into a dataSet or dataTable and doing a search in that dataSource.

As xero said, the switch from ADO to ADO.NET is "still just a learning process i need to get through". ADO.NET is more powerful (in my opinion anyway) and therefore simpler things may seem more complex.
 

xero

Member
Joined
Nov 8, 2004
Messages
12
Programming Experience
1-3
Well this is a little further down the line and i have figured out how to read in information to a textbox, with both databindings and Datareaders, but now the problem i am having is understand Databindings fully...

What i think databindings does it binds the data to a textbox lets say and if you change the textbox shouldn't that in theory change the data set that its bound to???

If not how can i go about doing this. I have a bunch of textboxes with Databindings to a Dataset thats being read in from a table. What I would like to do is be able to change the textbox and then update the table from there. How is this possible?

THANKS!
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
If you are using dataBindings, it should update once the text changes in the textbox.
It depends on how you have it set up as to when the changes are made in the DataTable. Usually when the position of the BindingManagerBase is changed the changes are made in the dataTable. You may have to call BindingManagerBase.EndCurrentEdit.

As always with using dataBindings, I suggest using the DataForm Wizard provided by Visual Studio as a starting point to learning how to use dataBindings. You answer question provided by the Wizard (which connection to use; which tables and fields you want included; dataGrid or seperate controls; navigation controls; update, cancel buttons; ...) then the wizards creates the typed dataset and all the code. You can the check out the code to see how it's done and modify code to suit your needs.
 

cjaymcmeans

Well-known member
Joined
Jan 12, 2005
Messages
85
Programming Experience
3-5
hi..
try this... it works if you dont want to bind...

dim myxs() as datarow
txtfname.Text = (MyXs(0).Item("userfname").ToString)
txtlname.Text = (MyXs(0).Item("userlname").ToString)
txtdate.Text = (MyXs(0).Item("userdate").ToString)
 

xero

Member
Joined
Nov 8, 2004
Messages
12
Programming Experience
1-3
I never followed up on this, but i just wanted to let you guys knowwhat i did. I have now become considerably more knowledgable withADO.net. What i did was just use the datareader for the textboxes thati wanted to populate the information into the fields. And as far as theinformation that has multiple lines I just used the data adapter tofill a datagrid and create datatables wherever necessary.... You guyshelped me considerably and i appreciate it.

Thanks!
 
Top