|
-
Sep 25th, 2006, 03:34 AM
#1
Thread Starter
Hyperactive Member
[02/03] Problems updating database
Okay I have a bound datagrid and some bound text boxes like so.
VB Code:
Me.txtmon.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.mon"))
Me.txttue.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.tue"))
Me.txtwed.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.wed"))
Me.txtthu.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.thur"))
Me.txtfri.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.fri"))
Me.txtwk.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.wk"))
Me.dgemployees.DataSource = ds
Me.dgemployees.DataMember = "Employees"
If I alter the datatable using the textboxes or the datagrid. The data in the datatable does change. My problem is when I call the update method like so
VB Code:
daemployees.Update(ds, "Employees")
The data always updates back to the databse if I made the change in the datagrid, but it doesn't allways update if I used the textboxes.
V confused, any help would be great.
-
Sep 25th, 2006, 06:19 AM
#2
Thread Starter
Hyperactive Member
Re: [02/03] Problems updating database
Okay to add more detail, I've been doing a bit of investigating. If I change a record in the text box then run the following
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim xDataTable As DataTable = ds.Tables("employees").GetChanges(DataRowState.Modified)
Dim dr As DataRow
For Each dr In xDataTable.Rows
MsgBox(dr("name"))
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
I get nothing, but if I change a record then move to a different record in the datatable and then run the above I get a match. So my question is how do I let the datatable know it has been updated without moving between records?
-
Sep 29th, 2006, 09:41 AM
#3
Thread Starter
Hyperactive Member
Re: [02/03] Problems updating database
-
Sep 29th, 2006, 10:01 AM
#4
Thread Starter
Hyperactive Member
Re: [02/03] Problems updating database
Okay if I do this
VB Code:
Me.BindingContext(ds, "employees").Position = Me.BindingContext(ds, "employees").Position
daemployees.Update(ds, "Employees")
It works, but just seems wrong and silly.
-
Oct 6th, 2006, 08:08 AM
#5
Thread Starter
Hyperactive Member
Re: [02/03] Problems updating database
Er, anyone?
Just seems like this should be simple.
Let me know if you need anymore detail.
-
Oct 6th, 2006, 08:57 AM
#6
Banned
Re: [02/03] Problems updating database
One of the reasons why you are not getting help is because you are using bound controls. Bound controls are evil IMHO, and I'd rather write more code to avoid bound controls then ever think about using bound controls. Consider not using bound controls and use unbound controls. You'll write a lot more code but in the end you'll be better off (less exceptions, performance will be much better).
-
Oct 6th, 2006, 09:24 AM
#7
Re: [02/03] Problems updating database
Data-binding is a good thing in my opinion but just like anything, if you don't understand what's happening you can't fix it when issues arise. If you know how to use it properly data-binding makes setting up complex forms simple. You could save yourself hours of coding. By the same token, if you don't do it properly and you don't understand how it works then you could cost yourself hours. To say that avoiding data-binding will redsuce the number of exceptions and dramatically increase performance are both completely false. Properly written code will run without exceptions whether you use data-binding or not, and a data-bound form will spend far more time communicating with the database and waiting for the user to provide input than it will on anything relatted to data-binding. VB6 data-binding was dodgy but .NET data-binding is COMPLETELY UNLIKE VB6 data-binding. They are similar in name only. In .NET data-binding is a good thing and you gain nothing by not using it. I'm not saying that you can't choose not to use it if you want but I am absolutely saying that you don't benefit by that decision. You can understand the mechanics of dealing with data just as well if you use data-binding as if you don't. The key to that is to read and not assume.
Having said all that, some times it can be difficult to provide help when data-binding has been used because as there is no code to write it is not possible to show you the code that will fix your issue. It requires your explaining your issue clearly and our explaining the remedy clearly in return. That's not always easy to achieve.
-
Oct 6th, 2006, 10:24 AM
#8
Thread Starter
Hyperactive Member
Re: [02/03] Problems updating database
Okay to try and explain what I think is happening a bit more clearly.
If you have a datagrid bound to a datatable, you make a change in the datagrid you get a little pencil appear on the datagrid saying it is being edited. If you clieck on the pencil it saves your changes, or if you move away it saves you changes.
I belive the problem is that when I alter a value in a bound text box there is no pencil to click to signify to save the changes back to the datatable. So in effect I belive I need to duplicate what the pencil does.
-
Oct 6th, 2006, 10:45 AM
#9
Re: [02/03] Problems updating database
In .NET 2.0 you can set a Binding to update OnPropertyChanged or OnValidation. That means that if you bind a data field to the Text property you can force the data field to update each time the Text property changes or only when the TextBox is validated. In .NET 1.x you don't have that choice. I can't remember which it is but a Binding will only update one way .NET 1.x. I think it's OnValidation, which means that the TextBox must lose focus for the bound data field to be updated.
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
|