Results 1 to 14 of 14

Thread: Update changes in datatatable to data base

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Update changes in datatatable to data base

    i am having a datatable ( populated externally in a form & bound to a datagridview )
    how can i update the changes back to the database please

    please don't ask me to google it, i am bit tired
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

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

    Re: Update changes in datatatable to data base

    Quote Originally Posted by make me rain View Post
    please don't ask me to google it, i am bit tired
    Oh you poor dear. That Googling is exhausting so I can understand why you'd want us to make the effort for you. Perhaps we could fetch you a pipe and slippers too. Anyway, follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data. It includes several examples of saving changes from a DataTable to a database using a data adapter and various other ADO.NET examples besides.
    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

  3. #3
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: Update changes in datatatable to data base

    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  4. #4

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Update changes in datatatable to data base

    Oh you poor dear. That Googling is exhausting so I can understand why you'd want us to make the effort for you. Perhaps we could fetch you a pipe and slippers too.

    please don't ask me to google it, i am bit tired
    i mentioned that i was tired googled, ( ofcourse to the best of my english knowledge )
    Secondly in any part of the english world, the above para convey that to say Some one has to search in google and reply to me !
    Perhaps we could fetch you a pipe and slippers too.
    i felt it is over reaction and insult to me.
    my apology to those who are all hurt by my post, & i would like to reiterate to the forum that by profession i am a train driver and not programmer, it is just my hobby that's it
    in future i request one and all while replying please keep it in your soul And like me there are N number of guys

    what i asked was
    ( populated externally in a form & bound to a datagridview )
    A function of a class is returning a populated datatable
    and then it is being binded to a datagridview control
    do some changes

    now these changes need to be Updated back to the datatabse
    and here where i am unable perform
    .

    @jmc
    thanks for your link to your signature, it is pretty good and usefull
    but not working for me

    thanks one and all
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update changes in datatatable to data base

    it is pretty good and usefull but not working for me
    at the risk of seeming harsh... yeah, that doesn't do it... you've been here long enough to know that just doesn't cut it... that's like me telling you the train isn't moving... doesn't mean squat does it. Now if I say that the switch was in the open position and that the train went through in the wrong direction and the closure rail caused a derail... and THAT's why it's not running... well, heck, you'd know what to do then right? same thing ... you're code has derailed and you haven't given and further insight in to the problem.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Update changes in datatatable to data base

    thanks for reply tech, i was upset today out of my 5 years being here
    any way forget it
    my code is here
    vb.net Code:
    1. Private _BindingTable As New DataTable = QueryActivater.FillDataTable() '' class method that fills the datatable
    2.     '' and then binded in to a datagridview
    3.     '' via binding source object
    4.  
    5.     Private Sub SaveChanges()
    6.  
    7.         Dim SQL_upDate = <sql>
    8. UPDATE crewcurrentstatus SET crewcurrentstatus.UnderPr = @UnderPr,crewcurrentstatus.prhrs = @Prhrs,
    9.         crewcurrentstatus.undercasualty = @undercasualty WHERE crewcurrentstatus.Status_serla = @Status_serla;
    10.                          </sql>
    11.  
    12.  
    13.         Dim Sql_UpdateString As String = SQL_upDate.Value.ToString
    14.  
    15.         Dim Conn As New Connector '' // a class to open / close connection
    16.  
    17.         Dim Cnn_Connection As New MySqlConnection
    18.         Dim Da5468 As New MySqlDataAdapter '' here i have a problem as what SQL SELECT statement to include
    19.         '' when already a populated datat table is available
    20.  
    21.  
    22.         Dim Cmd5468 As New MySqlCommand(Sql_UpdateString, Cnn_Connection)
    23.  
    24.         With Cmd5468
    25.             .Parameters.Add("@UnderPr", MySqlDbType.Int16, 1, "UnderPr")
    26.             .Parameters.Add("@Prhrs", MySqlDbType.Int16, 2, "Prhrs")
    27.             .Parameters.Add("@undercasualty", MySqlDbType.Int16, 1, "undercasualty")
    28.             .Parameters.Add("@Status_serla", MySqlDbType.Int16, 1, "Status_serla")
    29.         End With
    30.  
    31.         Conn.Cobs_Connect(True, Cnn_Connection) '' open connection
    32.         With Da5468
    33.             .UpdateCommand = Cmd5468
    34.             .MissingSchemaAction = MissingSchemaAction.AddWithKey
    35.             .Fill(Me._BindingTable)
    36.             .Update(Me._BindingTable)
    37.         End With
    38.  
    39.         Conn.Cobs_Connect(False, Cnn_Connection) '' close connection
    40.         Cmd5468.Dispose()
    41.         Da5468.Dispose()
    42.  
    43.         '' but changes not reflection in datatabse
    44.  
    45.  
    46.     End Sub
    Last edited by make me rain; Apr 23rd, 2013 at 10:30 AM. Reason: missed HIGHLIGHT quote
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update changes in datatatable to data base

    there's your mistake....
    Code:
            With Da5468
                .UpdateCommand = Cmd5468
                .MissingSchemaAction = MissingSchemaAction.AddWithKey
                .Fill(Me._BindingTable)
                .Update(Me._BindingTable)
            End With
    You filled your data table just before updating it... so you when the update is called, all the records have not changed (since you just filled it...
    you should fill it, do your edits then update... don't re-fill it... you'll just get the same unedited rows back.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Update changes in datatatable to data base

    Changes to datatable is commited by calling acceptchanges method
    in datagridview cellendedit event

    i have commented out this line
    '.Fill(Me._BindingTable)
    But still not changes not reflecting
    any other advise pl
    Last edited by make me rain; Apr 23rd, 2013 at 11:07 AM. Reason: added additional info
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update changes in datatatable to data base

    you're NOT calling acceptchanges are you? If so, you shouldn't... that doesn't actually update the database... all that does is mark all rows in the data table as "unchanged" ... but it doesn't actually send anythign to the database... only .Update does...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Update changes in datatatable to data base

    vb.net Code:
    1. Private Sub Dgv_Container_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Dgv_Container.CellEndEdit
    2.  
    3.        
    4.  
    5.         With Me.Dgv_Container '' my datagridview
    6.  
    7.             .CommitEdit(DataGridViewDataErrorContexts.Commit)
    8.             .EndEdit()
    9.  
    10.             If .CurrentRow.Cells(1).Value = True Then
    11.                 UnderPr = True
    12.  
    13.  
    14.                 '  _BindingTable.AcceptChanges() '' this line also commented out
    15.                 ' _Binding.EndEdit()
    16.            
    17.             Else
    18.                 UnderPr = False
    19.             End If
    20.                
    21.  
    22.         End With
    23.  
    24.    
    25.  
    26.     End Sub

    but still no action
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update changes in datatatable to data base

    where did THIS code come from? and what does it have to do with the other code you posted? Don't make me break out the trout slap...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Update changes in datatatable to data base

    Ok ok tech

    this how my project is

    (1) form contains a datagridview
    (2) at the form level i have created
    (a)
    vb.net Code:
    1. Private _BindingSource as new bindingsource
    2.                                         Private _BindingTable as new datatable

    in form load event, i have populated the _BindingTable by using a function in class
    vb.net Code:
    1. _BindingTable = QueryActivater.FillDataTable()
    2. _BindingSource.DataSource = _BindingTable
    3. Me.Dgv_Container.DataSource = _BindingTable '' binded the datagridview
    4. me.

    now i want make the changes to the datatable which user does in the datagridview, hence i used
    datagridview cellendedit event ( that's i posted above )
    vb.net Code:
    1. Private Sub Dgv_Container_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Dgv_Container.CellEndEdit
    2.  
    3.        
    4.  
    5.         With Me.Dgv_Container '' my datagridview
    6.  
    7.             .CommitEdit(DataGridViewDataErrorContexts.Commit)
    8.             .EndEdit()
    9.  
    10.             If .CurrentRow.Cells(1).Value = True Then
    11.                 UnderPr = True
    12.  
    13.  
    14.                 '  _BindingTable.AcceptChanges() '' this line i commented out after post # 9
    15.                 ' _Binding.EndEdit()
    16.            
    17.             Else
    18.                 UnderPr = False
    19.             End If
    20.                
    21.  
    22.         End With
    23.      
    24.     End Sub

    now i want to update the database with the changes made in the datatable ( i,e _BindingTable changes to database )
    and that is this code

    vb.net Code:
    1. Private Sub SaveChanges()
    2.  
    3.         Dim SQL_upDate = <sql>
    4. UPDATE crewcurrentstatus SET crewcurrentstatus.UnderPr = @UnderPr,crewcurrentstatus.prhrs = @Prhrs,
    5.         crewcurrentstatus.undercasualty = @undercasualty WHERE crewcurrentstatus.Status_serla = @Status_serla;
    6.                          </sql>
    7.  
    8.  
    9.         Dim Sql_UpdateString As String = SQL_upDate.Value.ToString
    10.  
    11.         Dim Conn As New Connector '' // a class to open / close connection
    12.  
    13.         Dim Cnn_Connection As New MySqlConnection
    14.         Dim Da5468 As New MySqlDataAdapter '' here i have a problem as what SQL SELECT statement to include
    15.         '' when already a populated datat table is available
    16.  
    17.  
    18.         Dim Cmd5468 As New MySqlCommand(Sql_UpdateString, Cnn_Connection)
    19.  
    20.         With Cmd5468
    21.             .Parameters.Add("@UnderPr", MySqlDbType.Int16, 1, "UnderPr")
    22.             .Parameters.Add("@Prhrs", MySqlDbType.Int16, 2, "Prhrs")
    23.             .Parameters.Add("@undercasualty", MySqlDbType.Int16, 1, "undercasualty")
    24.             .Parameters.Add("@Status_serla", MySqlDbType.Int16, 1, "Status_serla")
    25.         End With
    26.  
    27.         Conn.Cobs_Connect(True, Cnn_Connection) '' open connection
    28.         With Da5468
    29.             .UpdateCommand = Cmd5468
    30.             .MissingSchemaAction = MissingSchemaAction.AddWithKey
    31.            ' .Fill(Me._BindingTable)
    32.             .Update(Me._BindingTable)
    33.         End With
    34.  
    35.         Conn.Cobs_Connect(False, Cnn_Connection) '' close connection
    36.         Cmd5468.Dispose()
    37.         Da5468.Dispose()
    38.  
    39.        
    40.     End Sub
    till now i did this much
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

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

    Re: Update changes in datatatable to data base

    If it's not working for you then you didn't do it like I did. It's that simple. In my examples there's not an AcceptChanges in sight, so you added that arbitrarily. I also don;t have a Fill and Update call together anywhere, so to say that my code is not working for you is simply not the case.

    Anyway, I would consider the CellEndEdit event of the grid to be the wrong event to handle. If you really must save after every change, which I'd generally recommend against, then I'd suggest handling the CurrentItemChanged event of the BindingSource. Call EndEdit on the BindingSource and then Update on the data adapter.
    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

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update changes in datatatable to data base

    The other thing I noticed was that there was no call to the SaveChanges method...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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