none
can you help me with error in databinding?

    Question

  • hi everyone here... i'm creating a project and i used databinding, 

    I made a database and have tables and i used data source then dropped them on the form. When I run the debug and create a record save it, and then change the record and save it again I receive Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. i don't know why is that happening. i tried again the procedure i made to another form and i just run,create and save it i didn't change and there is no error occurs but when the second run of the debug i create a record then save it and then change the record and save it again the error occurs again. what should i do to fix that? i am a newbie and hope you can help me here

    this is the code back in the form: (the italic font is the error)

    Me.Validate()

    Me.StudentBindingSource.EndEdit()

    Me.TableAdapterManager.UpdateAll(Me.DataSet1)

    thanks in advance.

    Wednesday, January 08, 2014 3:17 AM

Answers

  • Use a command builder (from the database library you are using) like the code below.  The command builder will automatically generate an Insert, Delete, Update command from a SELECT command.  You need an Insert, Delete, and Update for the dataadapter update() to work.

    Dim sitedb As String = BookingApp.Globals.siteDB
        Dim conn As New SqlConnection(sitedb)
        Dim sql As String = "select * from name where BOOKING_REF = 'H2124'"
    
        Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim cb As SqlCommandBuilder
        Dim dt As New DataTable
        Dim c As Integer
    
        cmd = New SqlCommand(sql, conn)
        da = New SqlDataAdapter(cmd)
        cb = New SqlCommandBuilder(da)
        da.Fill(dt)
    
        For c = 0 To dt.Rows.Count - 1
    
            dt.Rows(c)!OVLREF3 = c
            dt.AcceptChanges()
        Next
    
        da.Update(dt)


    jdweng


    Wednesday, January 08, 2014 3:47 AM
  • Probably you use this code in an event and do it more time. The concurrency information is then updated on the disk but not in your data.

    Try it by changing your code to this.

    Me.TableAdapterManager.UpdateAll(Me.DataSet1)
    DataSet1.Clear
    Me.TableAdapterManager.Fill(DataSet1)


    Success
    Cor

    Wednesday, January 08, 2014 9:18 AM

All replies

  • Use a command builder (from the database library you are using) like the code below.  The command builder will automatically generate an Insert, Delete, Update command from a SELECT command.  You need an Insert, Delete, and Update for the dataadapter update() to work.

    Dim sitedb As String = BookingApp.Globals.siteDB
        Dim conn As New SqlConnection(sitedb)
        Dim sql As String = "select * from name where BOOKING_REF = 'H2124'"
    
        Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim cb As SqlCommandBuilder
        Dim dt As New DataTable
        Dim c As Integer
    
        cmd = New SqlCommand(sql, conn)
        da = New SqlDataAdapter(cmd)
        cb = New SqlCommandBuilder(da)
        da.Fill(dt)
    
        For c = 0 To dt.Rows.Count - 1
    
            dt.Rows(c)!OVLREF3 = c
            dt.AcceptChanges()
        Next
    
        da.Update(dt)


    jdweng


    Wednesday, January 08, 2014 3:47 AM
  • Probably you use this code in an event and do it more time. The concurrency information is then updated on the disk but not in your data.

    Try it by changing your code to this.

    Me.TableAdapterManager.UpdateAll(Me.DataSet1)
    DataSet1.Clear
    Me.TableAdapterManager.Fill(DataSet1)


    Success
    Cor

    Wednesday, January 08, 2014 9:18 AM
  • hi sir... i so sorry to say this but its complicated for me to understand the code, hope you understand. can i request? can you put some comments for (like BookingApp.Globals.siteDB) or everyting you put there.

    thanks :)

    Thursday, January 09, 2014 3:40 AM
  • hi sir the Fill in Me.TableAdaterManager.Fill(DataSet1) it says that is not a member in the DataSet1TableAdapters. is there any code i forgot to put on?

    Thursday, January 09, 2014 3:54 AM
  • I think you have an issue with the difference between a DataSet and a DataTable.  A DataSet consists of multiple DataTabes. 

    Dim ds as new DataSet

    ds.SetName = "Set 1"

    Dim table as new DataTable

    table.TableName = "Table 1"

    ds.Tables.Add(table)

    You can then access the table 3 different ways

    1) table

    2) ds.Tables(0)

    3) ds.Tables("Table 1")


    jdweng

    Thursday, January 09, 2014 9:25 AM
  • i am sorry sir for that, i don't know what that means so i made this from friends


        Private Sub btnSaveData_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveData.Click
            If DataSet1.HasChanges Then
                Try
                    If MsgBox(" Save Changes?", MsgBoxStyle.OkCancel, Me.Text) = DialogResult.OK Then
                        Me.TableAdapterManager.UpdateAll(Me.DataSet1)
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    End Sub

    it works, but it doesn't update the data i've saved
    it only updates if i am going to add another data
    Friday, January 10, 2014 8:30 PM
  • You need to use a commandbuilder like the code I posted on Jan 8.  The issue isn't with the code you just posted.  The issue is with the code you used to query the database.  

    jdweng

    Friday, January 10, 2014 8:35 PM
  • thank you sir... can you put some comments what is the meaning of  (like BookingApp.Globals.siteDB) or everyting you put there. i am sorry sir but i can't understand.... thanks a lot :)
    Friday, January 10, 2014 9:02 PM
  • thank you sir... can you put some comments what is the meaning of  (like BookingApp.Globals.siteDB) or everyting you put there. i am sorry sir but i can't understand.... thanks a lot :)
    BookingApp.Globals.siteDBis just a content of a string variable, you just need to focus on the way binding to your dataset or datatable.

    remember make the reply as answer and vote the reply as helpful if it helps.

    Monday, January 13, 2014 6:30 AM
  • hi sir the Fill in Me.TableAdaterManager.Fill(DataSet1) it says that is not a member in the DataSet1TableAdapters. is there any code i forgot to put on?

    Then use the method you use to retrieve the file. I see now that I was even able to use the right dataset name you where using, without that you had showed one bit of code.


    Success
    Cor

    Monday, January 13, 2014 12:55 PM