dcsimg
Results 1 to 4 of 4

Thread: Vb.net - Query Update [Microsoft Access]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Question Vb.net - Query Update [Microsoft Access]

    Hello everyone,
    when I want to edit values in the datagridview and then click on my save button, it does not save the edit in my access table.
    Could someone say me what did I wrote wrong.
    Thanks in advance!


    My code is this :


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i As Integer = ArbeitDataGridView.CurrentRow.Index

    Dim Planer As String = ArbeitDataGridView.Item(1, i).Value
    Dim ProjektName As String = ArbeitDataGridView.Item(2, i).Value
    Dim Prioritšt As String = ArbeitDataGridView.Item(3, i).Value
    Dim Datum As Date = ArbeitDataGridView.Item(4, i).Value
    Dim Beschreibung As String = ArbeitDataGridView.Item(5, i).Value
    Try

    ArbeitTableAdapter1.UpdateQuery(ProjektName, Prioritšt, Datum, Beschreibung, Planer)
    Me.ArbeitTableAdapter1.Fill(Me.Arbeitseinstellung.Arbeit)

    Catch ex As Exception
    MsgBox(ex.Message)
    End Try

    End Sub
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    My query :

    UPDATE Arbeit
    SET Projektname = ?, Prioritšt = ?, Datum = ?, Beschreibung = ?
    WHERE (Planer = ?)

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,754

    Re: Vb.net - Query Update [Microsoft Access]

    I don't know exactly why but you are one of a large number of people who try to do things the wrong way around. Don't try to save data separately and retrieve it again. You already have the data otherwise you wouldn't be able to save it, so there's no need for you to retrieve it. What you should be doing:

    1. Call Fill on your table adapter to populate your DataTable at startup, e.g. in the Load event handler of the form. You're probably already doing this. If you dragged a table onto the form to generate the grid then it probably was done for you.
    2. Bind your DataTable to your grid via a BindingSource. If you dragged a table onto the form then this would have been done for you.
    3. Edit the data in the grid via the UI. You're presumably already doing this.
    4. To save data, call Update on the same table adapter. THIS IS WHAT YOU'RE MISSING!

    Step 4 is the crux of the matter. You don't separate out the changes and save them, then retrieve the data from the database again. The changes are already in your DataTable and you simply save them from there and then there's no ned to retrieve the data again.

    If you need values generated by the Access database, e.g. primary key valu4es from an AutoNumber column, then you can retrieve them specifically via a mechanism designed for that purpose. To learn how to do that, follow the CodeBank link in my signature below and check out my thread on Retrieving AutoNumber Values or the like.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Re: Vb.net - Query Update [Microsoft Access]

    I wrote this in the button
    -> ArbeitTableAdapter1.Update(Arbeitseinstellung.Arbeit)
    But it still don't work.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,754

    Re: Vb.net - Query Update [Microsoft Access]

    Most likely it does work, but you're just not looking for the changes properly. It's a very common mistake. When you call that Update method, there are three possible outcomes:

    1. The call fails and an exception is thrown. The exception will provide information indicating why the call failed.
    2. The call succeeds and returns zero. This indicates that there were no changes in the DataTable to save.
    3. The call succeeds and returns a non-zero value. This indicates that there were changes to save and they were saved.

    Your first task is to determine which of those outcomes is occurring in your case. If it's 1 then, as I suggested, you need to look at the exception to determine why. If it's 2 then you need to determine why there are no changes in your DataTable. If it's 3 - and it usually is - then it's working as it should and you just don;t understand how local data files are managed, to learn that, follow the first link in my signature below.

    For future reference, statements like "it still don't work" are not adequate explanations of anything. You need to provide ALL the relevant information. That would include EXACTLY what did happen and EXACTLY what you did to determine that it didn't work as you expected. If we know what you did then we can likely work out what you did wrong.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width