please help about update button

aasaleh

New member
Joined
Nov 20, 2008
Messages
3
Programming Experience
Beginner
this is my first visit to the forum
i need a help to change this code to update my database becaus this code make new record but i need to update exist record

VB.NET:
    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'save record
        Dim newRecord As DataRow = Me.DataSet61.PHONEBOOK.NewRow

        newRecord("PAYROLL") = Me.TextBox11.Text
        newRecord("NAME") = Me.TextBox10.Text
        newRecord("SECTOR") = Me.TextBox9.Text
        newRecord("PHONE") = Me.TextBox8.Text
        newRecord("EMAIL") = Me.TextBox7.Text
        newRecord("LOCATION") = Me.TextBox3.Text
        newRecord("DEPARTMENT") = Me.TextBox4.Text

        Me.DataSet61.PHONEBOOK.Rows.Add(newRecord)
        Me.OleDbDataAdapter1.Update(Me.DataSet61)
        

    End Sub
 
Well, if you call NewRow and then Add are you surprised that it adds a new record? If you want to edit an existing record then you have to first get an existing record from the table, which you do by indexing it:
VB.NET:
Dim existingRecord As DataRow = Me.DataSet61.PHONEBOOK.Rows(index)
 
This is a better way to code:

VB.NET:
    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'save record
        Dim newRecord As [B]DataSet61.PHONEBOOKRow[/B] = Me.DataSet61.PHONEBOOK.[B]NewPHONEBOOKRow()[/B]

        newRecord.[B]PAYROLL [/B]= Me.TextBox11.Text
        newRecord.[B]NAME[/B]. = Me.TextBox10.Text
        newRecord.[B]SECTOR[/B]. = Me.TextBox9.Text
        newRecord.[B]PHONE[/B]. = Me.TextBox8.Text
        newRecord.[B]EMAIL[/B]. = Me.TextBox7.Text
        newRecord.[B]LOCATION[/B]. = Me.TextBox3.Text
        newRecord.[B]DEPARTMENT[/B]. = Me.TextBox4.Text

        Me.DataSet61.PHONEBOOK.[B]AddPHONEBOOKRow[/B](newRecord)
        Me.OleDbDataAdapter1.Update(Me.DataSet61)
        

    End Sub

Please name yout buttons and textboxes something better than "Button1" "Button2"
There are few things more frustrating than trying to help someone out in a forum, when the code is just full of meaningless variable names and "magic numbers"
 

Latest posts

Back
Top