textbox databinding (which is better?)

thejeraldo

Well-known member
Joined
Jun 9, 2009
Messages
70
Location
Philippines
Programming Experience
1-3
uhm.. which is a better method to use in databinding?

textbox.text = dataset.tables(0).rows(0).item(0).tostring

or

textbox.databindings.add("text",dataset.tables(0)."employeename")

???

thanks...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
Given that the first option isn't using data-binding at all, the only answer is the second option. Data-binding means that the value in the control and the value in the data source are bound together, i.e. whatever affects one will affect the other. That first code snippet is simply assigning a value manually. Any changes to either the control or the data source would require another manual assignment to update the other. Using the second option, i.e. data-binding, any changes you make to the data in the control will be automatically pushed back to the data source and vice versa.
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
The first is a bad way to do it in nearly all situations; it relies on position (which can change) and will crash if there is no data to be had at that position
 

thejeraldo

Well-known member
Joined
Jun 9, 2009
Messages
70
Location
Philippines
Programming Experience
1-3
thanks for the answer guys.. now i might have to change my codes a little. :)
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
In addition you may not need binding at all.
DataReader object will be probably more efficient for you.
Or even better (if you use MSSQL) .. OUTPUT parameter(s).
Just execute command and get the value of the parameter.

Code:
Me.TextBox1.Text = command.Parameters("employeename").Value.ToString()
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
Just to elaborate on kulrom's post, data-binding would normally be used if the data was to be retrieved, displayed, edited and saved, especially if there were multiple records. If there's no editing and saving then you would probably still use data-binding if there were multiple records, although it would depend on the specific circumstances. If there's only one record to display with no editing then data-binding is a bit pointless.
 

thejeraldo

Well-known member
Joined
Jun 9, 2009
Messages
70
Location
Philippines
Programming Experience
1-3
uhmm.. do i need to have a binding if my method of saving an edited or new record is by means of an oledbcommand then executing an update/insert statement which is kinda like:

"UPDATE tblemp set EmpID='" + textbox1.text + "'" ??

or if you guys are saying that i dont need binding? then what do Pros like you use to show data from a record on a form??
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
uhmm.. do i need to have a binding if my method of saving an edited or new record is by means of an oledbcommand then executing an update/insert statement which is kinda like:

"UPDATE tblemp set EmpID='" + textbox1.text + "'" ??

or if you guys are saying that i dont need binding? then what do Pros like you use to show data from a record on a form??
If you've only got one record then I probably wouldn't use data-binding but if you have more than one then I would.
 
Top Bottom