dataadapter does not update a Database (using VS 2005)

I've created a windows form with some fields automatically binded by the VS and created the command buttons to change the data, using the TableBindingSource to make the changes to the dataset, and so the dataadapter.update(table) to make it reflect on my database, but i'm having a problem. I can Delete or change records, but when i'm inserting a new one, it goes to the Dataset but does not to the data base.
here goes the part of code:


note: before it, i've called TclientsBindingSource.addnew() which cleans my form and creates a new row in my datagrid .... so when the user fill in the form and Press Save it calls the code bellow. Please, someone help me. there's something wrong

Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
      If validateAllFields() Then
            Try
                     M
e.TclientsBindingSource.EndEdit()
                     Dim res As Integer = Me.TClientsTableAdapter.Update(Me.DsContas.TClients)
                  
   MsgBox(res & " record inserted / changed !") ' just for a test
                  
   ' Me.TClientesBindingSource.ResetBindings(False) 
                     BtnAddNew.Enabled = True
                     dgClientes.ReadOnly =
False
                     CbNome.Enabled =
True
                     CbIdCli.Enabled =
True
                     BtnDelete.Enabled =
True
                     dgClientes.Update()
                     dgClientes.Enabled =
True
                     BtnPrevious.Enabled =
True
                     dgClientes.Focus()
         Catch ex As Exception
                     MsgBox(ex.Message)
         End Try
   End If

End Sub
                                                         Thank you all!



Answer this question

dataadapter does not update a Database (using VS 2005)

  • JonneeZ

    Hi Paul,

    Thanks.
    I looked the update statement like you told me, and it appears to be correct.
     But it makes me to think about: since I'm trying to insert a new record, should not I use the tableAdapter.Insert method
    The Uptade, needs at least one column (PK) to be passed, and since the registry does not exists yet it wont function... I'm right
    So, I've taken a look at the insert method  and trying to use it . In this case I have to pass all the fileds values to this method... but its ok. And worked, by this way .
    Is it the best way ( because dont appears tobe the easiest) or it has to function correctly with the Update

    Well,
    Thaks again!

  • velteyn

    Hi,


    Try checking the UpdateCommand of the Adapter. Check if it generated the right Update Statement...

    If you think that it's right and still won't execute, try posting the Update Statement...

    Here's also a link that discusses the same problem.
    http://forums.microsoft.com/msdn/ShowPost.aspx PostID=10267




    cheers,


    Paul June A. Domag

  • Jus55

    Hi Paul,

    there was an acceptchanges and a problem with the type of data I was trying to put in a "currency" field (it was waiting for "decimal" and I was passing a "double" var)... now it's solved... everything is okay!

    Thanks!

    LC

  • M.Forsyth

    Hi,


    Ideally, you'll only have to use the Adapter.Update() method. If there were changes in your datatable, the adapter would call the specified Insert Statement (if there's a new record) and Update Statement (if there are record changes)...

    Try playing with the Insert and Update Statements and see if both are correct. And be sure that there are no AcceptChanges being called in your datatable, since the adapter wouldn't know if there were really changes being made...





    cheers,


    Paul June A. Domag

  • dataadapter does not update a Database (using VS 2005)