Results 1 to 9 of 9

Thread: [02/03] Problems updating database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    [02/03] Problems updating database

    Okay I have a bound datagrid and some bound text boxes like so.


    VB Code:
    1. Me.txtmon.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.mon"))
    2.         Me.txttue.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.tue"))
    3.         Me.txtwed.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.wed"))
    4.         Me.txtthu.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.thur"))
    5.         Me.txtfri.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.fri"))
    6.         Me.txtwk.DataBindings.Add(New System.Windows.Forms.Binding("Text", ds, "employees.wk"))
    7.  
    8.  
    9.   Me.dgemployees.DataSource = ds
    10.         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:
    1. 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.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         Try
    4.  
    5.  
    6.         Dim xDataTable As DataTable = ds.Tables("employees").GetChanges(DataRowState.Modified)
    7.         Dim dr As DataRow
    8.  
    9.         For Each dr In xDataTable.Rows
    10.  
    11.             MsgBox(dr("name"))
    12.  
    13.  
    14.             Next
    15.         Catch ex As Exception
    16.             MsgBox(ex.ToString)
    17.         End Try
    18.     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?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: [02/03] Problems updating database

    Bump, anyone?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: [02/03] Problems updating database

    Okay if I do this

    VB Code:
    1. Me.BindingContext(ds, "employees").Position = Me.BindingContext(ds, "employees").Position
    2.  
    3. daemployees.Update(ds, "Employees")

    It works, but just seems wrong and silly.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: [02/03] Problems updating database

    Er, anyone?
    Just seems like this should be simple.
    Let me know if you need anymore detail.

  6. #6
    Banned
    Join Date
    May 2006
    Posts
    161

    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).

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    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.

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

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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