none
Database related problem

    Question

  • Hello,

    I have make a simple vb application with ms access 2000 mdb database. I have added a datagridview in this application.

    I have write this code to button 1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RECBindingSource.AddNew()
        End Sub

    and this code to button 2

    rivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Try
                Me.Validate()
                Me.RECBindingSource.EndEdit()
                Me.RECTableAdapter.Update(Me.TH2000DataSet.REC)
                MsgBox("saved")
            Catch ex As Exception
                MsgBox("failed")
            End Try
        End Sub

    I have connected database with the help of wizard. It also attached to DGV and textbox.

    Problem occurs when i enter the values in textbox and press button 1, DGV not shown the first record. After that when i repeat this process DGV updates successfully. But when i press button 2 i got "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." error. I know that this is because of second record is inserted on first record. But why i got the error in the adding the first record..???

    • Changed type dharmang1910 Wednesday, April 5, 2017 1:13 PM New to Thread
    Tuesday, April 4, 2017 12:43 PM

Answers

  • Hi,

    I've got solution for my problem. Here is my code.

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Me.Validate()
                Me.RecBindingSource.EndEdit()
                Me.RecTableAdapter.Update(Me.TellerHelperDataSet.rec)
                Me.RecTableAdapter.Fill(Me.TellerHelperDataSet.rec)
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            RecBindingSource.AddNew()
        End Sub

    • Marked as answer by dharmang1910 Tuesday, April 11, 2017 12:19 PM
    Tuesday, April 11, 2017 12:19 PM

All replies

  • You can't type into bound textboxes and then click Button1 - you have to create a new row first, then edit the values for that row.

    When you press button1, you should get a new blank row in the datagridview.  With the new blank row selected, you should then be able to edit the values through any bound textboxes.

    If it doesn't work that way then you likely have something wrong with your databindings, tableadapter queries, or database design (e.g. primary key issues).


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, April 6, 2017 12:29 PM
    Moderator
  • That is really strange because with your code you should get "Failed" if there is something wrong.

    Success
    Cor

    Thursday, April 6, 2017 12:43 PM
  • Hi,

    I've got solution for my problem. Here is my code.

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Me.Validate()
                Me.RecBindingSource.EndEdit()
                Me.RecTableAdapter.Update(Me.TellerHelperDataSet.rec)
                Me.RecTableAdapter.Fill(Me.TellerHelperDataSet.rec)
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            RecBindingSource.AddNew()
        End Sub

    • Marked as answer by dharmang1910 Tuesday, April 11, 2017 12:19 PM
    Tuesday, April 11, 2017 12:19 PM