I'm using the following code to update my records in the database:
Imports System.Data.OleDb
Public Class Form1
Private Sub update_Click(sender As Object, e As EventArgs) Handles updateBtn.Click
'/// UPDATING TABLE - employee
Try
Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
conn.Open()
Dim command As New OleDbCommand("UPDATE employee SET [Last_Name] = @lname, [First_Name]= @fname, MI = @mi, Bday = @bday, Age = @age, [Nationality] = @natio, [Contact_Number] = @contact, Sex = @gender, Address = @address, Department = @department, [Position] = @position, TIN = @tin, SSS = @sss, Pagibig = @pagibig, PhilHealth = @phh, Termi_Resi = @termiresi, [Date_hired] = @datehired, [Rate_Month] = @rm, [Date_End] = @dateTermiResi, Status = @status, [Father_Name] = @father, [Mother_Name] = @mother, Civil = @civil, Spouse = @spouse, [Number_of_Children] = @numberchild, Child1 = @child1, Child2 = @child2, Child3 = @child3, Child4 = @child4, Child5 = @child5, Child6 = @child6, Child7 = @child7, Child8 = @child8, Child9 = @child9 WHERE [EmployID] = @numberemp", conn)
With command.Parameters
.AddWithValue("@numberemp", numberemp.Text)
.AddWithValue("@lname", lname.Text)
.AddWithValue("@fname", fname.Text)
.AddWithValue("@mi", mi.Text)
.AddWithValue("@bday", bday.Value)
.AddWithValue("@age", age.Text)
.AddWithValue("@natio", natio.Text)
.AddWithValue("@contact", contact.Text)
.AddWithValue("@gender", gender.Text)
.AddWithValue("@address", address.Text)
.AddWithValue("@department", department.Text)
.AddWithValue("@position", position.Text)
.AddWithValue("@tim", tin.Text)
.AddWithValue("@sss", sss.Text)
.AddWithValue("@pagibig", pagibig.Text)
.AddWithValue("@phh", phh.Text)
.AddWithValue("@termiresi", termiresi.Text)
.AddWithValue("@datehired", datehired.Value)
.AddWithValue("@rm", rm.Text)
.AddWithValue("@dateTermiResi", dateTermiResi.Value)
.AddWithValue("@status", status.Text)
.AddWithValue("@father", father.Text)
.AddWithValue("@mother", mother.Text)
.AddWithValue("@civil", civil.Text)
.AddWithValue("@spouse", spouse.Text)
.AddWithValue("@numberchild", numberchild.Text)
.AddWithValue("@child1", child1.Text)
.AddWithValue("@child2", child2.Text)
.AddWithValue("@child3", child3.Text)
.AddWithValue("@child4", child4.Text)
.AddWithValue("@child5", child5.Text)
.AddWithValue("@child6", child6.Text)
.AddWithValue("@child7", child7.Text)
.AddWithValue("@child8", child8.Text)
.AddWithValue("@child9", child9.Text)
End With
command.ExecuteNonQuery()
MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
command.Dispose()
conn.Close()
clearall()
RefreshDGV()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Whenever I click the update button, it says
Employee's Informations Successfuly Updated!
but in the database, it's not. Am I doing it wrong?
Always check the result of command.ExecuteNonQuery()
. This will return the number of rows modified.
Dim rows = command.ExecuteNonQuery()
If rows = 0 then
MessageBox.Show("No rows modified")
Else
MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
If rows = 0
then the problem is most likely with the where
clause. In this case it's matching on the parameter.
.AddWithValue("@numberemp", numberemp.Text)
Try using
.AddWithValue("@numberemp", Integer.Parse(numberemp.Text))
To make sure it's the right type.
Also some database providers don't use the names you give for parameters. The only care about the order. So put @numberemp
at the bottom of your parameter list as it's the last in the query.