更新记录时未修改任何行

时间:2016-09-21 07:37:53

标签: vb.net datagridview tabcontrol

我有5个标签页,每页都有Datagridview。每个选项卡代表我数据库中的一个表。问题是,更新时,只更新第一个表。这是我的代码:

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("@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)
                .AddWithValue("@numberemp", numberemp.Text)
            End With
            command.ExecuteNonQuery()
            MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            command.Dispose()
            conn.Close()
            clearall()


        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    '/// UPDATING TABLE - education

    Try
        Using conn2 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
            conn2.Open()
            Dim command2 As New OleDbCommand("UPDATE education SET Level_Educ = @hlevelEduc, Degree = @degree, Post_Degree = @postdegree, Elem = @elem, Year_one = @year1, Highschool = @highschool, Year_two = @year2, College = @college, Year_three = @year3, Training_School = @trainingS, Year_four = @year4, Other_Educ = @otherEduc WHERE [EmployID] = @numberemp1", conn2)
            numberemp1.Text = numberemp.Text
            With command2.Parameters
                .AddWithValue("@hlevelEduc", hlevelEduc.Text)
                .AddWithValue("@elem", elem.Text)
                .AddWithValue("@year1", year1.Text)
                .AddWithValue("@highschool", highschool.Text)
                .AddWithValue("@year2", year2.Text)
                .AddWithValue("@college", college.Text)
                .AddWithValue("@year3", year3.Text)
                .AddWithValue("@degree", degree.Text)
                .AddWithValue("@postdegree", postdegree.Text)
                .AddWithValue("@trainingS", trainingS.Text)
                .AddWithValue("@year4", year4.Text)
                .AddWithValue("@otherEduc", otherEduc.Text)
                .AddWithValue("@numberemp1", numberemp1.Text)
            End With
            Dim rows = command2.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
            command2.Dispose()
            conn2.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    '/// UPDATING TABLE - employment

    Try

        Using conn3 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
            conn3.Open()
            Dim command3 As New OleDbCommand("UPDATE employment SET Cname1 = @cname1, Posi1 = @posi1, Dept1 = @dept1, Tm1 = @tm1, Reason1 = @reason1, Cname2 = @cname2, Posi2 = @posi2, Dept2 = @dept2, Tm2 = @tm2, Reason2 = @reason2, Cname3 = @cname3, Posi3 = @posi3, Dept3 = @dept3, Tm3 = @tm3, Reason3 = @reason3, Cname4 = @cname4, Posi4 = @posi4, Dept4 = @dept4, Tm4 = @tm4, Reason4 = @reason4, Cname5 = @cname5, Posi5 = @posi5, Dept5 = @dept5, Tm5 = @tm5, Reason5 = @reason5, Cname6 = @cname6, Posi6 = @posi6, Dept6 = @dept6, Tm6 = @tm6, Reason6 = @reason6 WHERE [EmployID] = @numberemp2", conn3)
            numberemp2.Text = numberemp.Text
            With command3.Parameters
                .AddWithValue("@cname1", cname1.Text)
                .AddWithValue("@posi1", posi1.Text)
                .AddWithValue("@dept1", dept1.Text)
                .AddWithValue("@tm1", tm1.Text)
                .AddWithValue("@reason1", reason1.Text)
                .AddWithValue("@cname2", cname2.Text)
                .AddWithValue("@posi2", posi2.Text)
                .AddWithValue("@dept2", dept2.Text)
                .AddWithValue("@tm2", tm2.Text)
                .AddWithValue("@reason2", reason2.Text)
                .AddWithValue("@cname3", cname3.Text)
                .AddWithValue("@posi3", posi3.Text)
                .AddWithValue("@dept3", dept3.Text)
                .AddWithValue("@tm3", tm3.Text)
                .AddWithValue("@reason3", reason3.Text)
                .AddWithValue("@cname4", cname4.Text)
                .AddWithValue("@posi4", posi4.Text)
                .AddWithValue("@dept4", dept4.Text)
                .AddWithValue("@tm4", tm4.Text)
                .AddWithValue("@reason4", reason4.Text)
                .AddWithValue("@cname5", cname5.Text)
                .AddWithValue("@posi5", posi5.Text)
                .AddWithValue("@dept5", dept5.Text)
                .AddWithValue("@tm5", tm5.Text)
                .AddWithValue("@reason5", reason5.Text)
                .AddWithValue("@cname6", cname6.Text)
                .AddWithValue("@posi6", posi6.Text)
                .AddWithValue("@dept6", dept6.Text)
                .AddWithValue("@tm6", tm6.Text)
                .AddWithValue("@reason6", reason6.Text)
                .AddWithValue("@numberemp2", numberemp2.Text)
            End With
             Dim rows = command3.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
            command3.Dispose()
            conn3.Close()
            clearall()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    '/// UPDATING TABLE - disciplinary

    Try

        Using conn4 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
            conn4.Open()
            Dim command4 As New OleDbCommand("UPDATE disciplinary SET Offense1 = @o1, Action1 = @ao1, Dateoff1 = @do1, Offense2 = @o2, Action2 = @ao2, Dateoff2 = @do2, Offense3 = @o3, Action3 = @ao3, Dateoff3 = @do3, Offense4 = @o4, Action4 = @ao4, Dateoff4 = @do4, Offense5 = @o5, Action5 = @ao5, Dateoff5 = @do5, Offense6 = @o6, Action6 = @ao6, Dateoff6 = @do6, Offense7 = @o7, Action7 = @ao7, Dateoff7 = @do7, Offense8 = @o8, Action8 = @ao8, Dateoff8 = @do8, Offense9 = @o9, Action9 = @ao9, Dateoff9 = @do9, Offense10 = @10, Action10 = @ao10, Dateoff10 = @do10 WHERE [EmployID] = @numberemp3 ", conn4)
            numberemp3.Text = numberemp.Text
            With command4.Parameters
                .AddWithValue("@o1", o1.Text)
                .AddWithValue("@ao1", ao1.Text)
                .AddWithValue("@do1", do1.Value)
                .AddWithValue("@o2", o2.Text)
                .AddWithValue("@ao2", ao2.Text)
                .AddWithValue("@do2", do2.Value)
                .AddWithValue("@o3", o3.Text)
                .AddWithValue("ao3", ao3.Text)
                .AddWithValue("@do3", do3.Value)
                .AddWithValue("@o4", o4.Text)
                .AddWithValue("@ao4", ao4.Text)
                .AddWithValue("@do4", do4.Value)
                .AddWithValue("@o5", o5.Text)
                .AddWithValue("@ao5", ao5.Text)
                .AddWithValue("@do5", do5.Value)
                .AddWithValue("@o6", o6.Text)
                .AddWithValue("@ao6", ao6.Text)
                .AddWithValue("@do6", do6.Value)
                .AddWithValue("@o7", o7.Text)
                .AddWithValue("@ao7", ao7.Text)
                .AddWithValue("@do7", do7.Value)
                .AddWithValue("@o8", o8.Text)
                .AddWithValue("@ao8", ao8.Text)
                .AddWithValue("@do8", do8.Value)
                .AddWithValue("@o9", o9.Text)
                .AddWithValue("@ao9", ao9.Text)
                .AddWithValue("@do9", do9.Value)
                .AddWithValue("@o10", o10.Text)
                .AddWithValue("@ao10", ao10.Text)
                .AddWithValue("@do10", do10.Value)
                .AddWithValue("@numberemp3", numberemp3.Text)
            End With
             Dim rows = command4.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
            conn4.Close()
            clearall()

        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    '/// UPDATING TABLE - trainings


    Try

        Using conn5 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
            conn5.Open()
            Dim command5 As New OleDbCommand("UPDATE trainings SET ts1 = @ts1, dts1 = @dts1, ts2 = @ts2, dts2 = @dts2, ts3 = @ts3, dts3 = @dts3, ts4 = @ts4, dts4 = @dts4, ts5 = @ts5, dts5 = @dts5, ts6 = @ts6, dts6 = @dts6, ts7 = @ts7, dts7 = @dts7, ts8 = @ts8, dts8 = @dts8, ts9 = @ts9, dts9 = @dts9, ts10 = @ts10, dts10 = @dts10, ts11 = @ts11, dts11 = @dts11, ts12 = @ts12, dts12 = @dts12, ts13 = @ts13, dts13 = @dts13, ts14 = @ts14, dts14 = @dts14, ts15 = @ts15, dts15 = @dts15, ts16 = @ts16, dts16 = @dts16, ts17 = @ts17, dts17 = @dts17, ts18 = @ts18, dts18 = @dts18, ts19 = @ts19, dts19 = @dts19, ts20 = @ts20, dts20 = @dts20, ts21 = @ts21, dts21 = @dts21, ts22 = @ts22, dts22 = @dts22, ts23 = @ts23, dts23 = @dts23, ts24 = @ts24, dts24 = @dts24 WHERE [EmployID] = @numberemp4 ", conn5)
            numberemp4.Text = numberemp.Text
            With command5.Parameters
                .AddWithValue("@ts1", ts1.Text)
                .AddWithValue("@dts1", dts1.Value)
                .AddWithValue("@ts2", ts2.Text)
                .AddWithValue("@dts2", dts2.Value)
                .AddWithValue("@ts3", ts3.Text)
                .AddWithValue("@dts3", dts3.Value)
                .AddWithValue("@ts4", ts4.Text)
                .AddWithValue("@dts4", dts4.Value)
                .AddWithValue("@ts5", ts5.Text)
                .AddWithValue("@dts5", dts5.Value)
                .AddWithValue("@ts6", ts6.Text)
                .AddWithValue("@dts6", dts6.Value)
                .AddWithValue("@ts7", ts7.Text)
                .AddWithValue("@dts7", dts7.Value)
                .AddWithValue("@ts8", ts8.Text)
                .AddWithValue("@dts8", dts8.Value)
                .AddWithValue("@ts9", ts9.Text)
                .AddWithValue("@dts9", dts9.Value)
                .AddWithValue("@ts10", ts10.Text)
                .AddWithValue("@dts10", dts10.Value)
                .AddWithValue("@ts11", ts11.Text)
                .AddWithValue("@dts11", dts11.Value)
                .AddWithValue("@ts12", ts12.Text)
                .AddWithValue("@dts12", dts12.Value)
                .AddWithValue("@ts13", ts13.Text)
                .AddWithValue("@dts13", dts13.Value)
                .AddWithValue("@ts14", ts14.Text)
                .AddWithValue("@dts14", dts14.Value)
                .AddWithValue("@ts15", ts15.Text)
                .AddWithValue("@dts15", dts15.Value)
                .AddWithValue("@ts16", ts16.Text)
                .AddWithValue("@dts16", dts16.Value)
                .AddWithValue("@ts17", ts17.Text)
                .AddWithValue("@dts17", dts17.Value)
                .AddWithValue("@ts18", ts18.Text)
                .AddWithValue("@dts18", dts18.Value)
                .AddWithValue("@ts19", ts19.Text)
                .AddWithValue("@dts19", dts19.Value)
                .AddWithValue("@ts20", ts20.Text)
                .AddWithValue("@dts20", dts20.Value)
                .AddWithValue("@ts21", ts21.Text)
                .AddWithValue("@dts21", dts21.Value)
                .AddWithValue("@ts22", ts22.Text)
                .AddWithValue("@dts22", dts22.Value)
                .AddWithValue("@ts23", ts23.Text)
                .AddWithValue("@dts23", dts23.Value)
                .AddWithValue("@ts24", ts24.Text)
                .AddWithValue("@dts24", dts24.Value)
                .AddWithValue("@numberemp4", numberemp4.Text)
            End With
             Dim rows = command5.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
            command5.Dispose()
            conn5.Close()
            disableall()
            clearall()

        End Using

    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
    Add1.Visible = True
    btnReg.Visible = True
    updateBtn.Visible = False
    RefreshDGV()
End Sub

我总是得到

  

没有修改行

将第二个更新到第五个表格。

这是我的表格,以明确它是否太混乱......

enter image description here

1 个答案:

答案 0 :(得分:0)

假设您想在需要时将数据加载到标签页:

  1. 使用Load事件加载Tap Page 1
  2. 的数据
  3. 挂钩事件选择TabControl以加载其他页面的数据

    private void tabControl1_Selecting(object sender,TabControlCancelEventArgs e){     if(e.TabPage.Name ==" TabPage1"){         //为标签加载数据第1页

        DataTable dt = new DataTable();
        dt.Load(reader);
        grid2.DataSource = dt;
    }
    

    }