如何保存检查和取消选中复选框从datagridview到数据库使用vb.net? [英] How to save a check and unchecked checkboxes from datagridview to the database using vb.net?

查看:125
本文介绍了如何保存检查和取消选中复选框从datagridview到数据库使用vb.net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Try
        myconnection.Open()
        'Declaration of Variables
        Dim str As String

        Dim vCourseCode As String
        Dim vSection As String
        Dim vSurname As String
        Dim vFirstName As String
        Dim vMiddleInitial As String
        Dim vDate1 As Boolean
        Dim vDate2 As Boolean
        Dim vDate3 As Boolean
        Dim vDate4 As Boolean
        Dim vDate5 As Boolean
        Dim vDate6 As Boolean
        Dim vDate7 As Boolean
        Dim vDate8 As Boolean
        Dim vDate9 As Boolean
        Dim vAbsent As String
        Dim vPresent As String
        Dim vEQ As String


        For x As Integer = 0 To DataGridView1.Rows.Count - 2
            vCourseCode = DataGridView1.Rows(x).Cells(1).Value
            vSection = DataGridView1.Rows(x).Cells(2).Value
            vSurname = DataGridView1.Rows(x).Cells(3).Value
            vFirstName = DataGridView1.Rows(x).Cells(4).Value
            vMiddleInitial = DataGridView1.Rows(x).Cells(5).Value 
            vAbsent = DataGridView1.Rows(x).Cells(6).Value
            vPresent = DataGridView1.Rows(x).Cells(7).Value
            vEQ = DataGridView1.Rows(x).Cells(8).Value
            vDate1 = DataGridView1.Rows(x).Cells(0).Value
            vDate2 = DataGridView1.Rows(x).Cells(1).Value
            vDate3 = DataGridView1.Rows(x).Cells(2).Value
            vDate4 = DataGridView1.Rows(x).Cells(3).Value
            vDate5 = DataGridView1.Rows(x).Cells(4).Value
            vDate6 = DataGridView1.Rows(x).Cells(5).Value
            vDate7 = DataGridView1.Rows(x).Cells(6).Value
            vDate8 = DataGridView1.Rows(x).Cells(7).Value
            vDate9 = DataGridView1.Rows(x).Cells(8).Value

            str = "Insert into StudentsAttendance ([CourseCode],[Section],[Surname],[FirstName],[MiddleName],[Date1],[Date2],[Date3],[Date4],[Date5],[Date6],[Date7],[Date8],[Date9],[Absent],[Present],[EQ]) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"


            Dim cmd As OleDbCommand = New OleDbCommand(str, myconnection)
            cmd.Parameters.AddWithValue("@CourseCode", vCourseCode)
            cmd.Parameters.AddWithValue("@Section", vSection)
            cmd.Parameters.AddWithValue("@Surname", vSurname)
            cmd.Parameters.AddWithValue("@FirstName", vFirstName)
            cmd.Parameters.AddWithValue("@MiddleName", vMiddleInitial)
            cmd.Parameters.AddWithValue("@Date1", vDate1)
            cmd.Parameters.AddWithValue("@Date2", vDate2)
            cmd.Parameters.AddWithValue("@Date3", vDate3)
            cmd.Parameters.AddWithValue("@Date4", vDate4)
            cmd.Parameters.AddWithValue("@Date5", vDate5)
            cmd.Parameters.AddWithValue("@Date6", vDate6)
            cmd.Parameters.AddWithValue("@Date7", vDate7)
            cmd.Parameters.AddWithValue("@Date8", vDate8)
            cmd.Parameters.AddWithValue("@Date9", vDate9)
            cmd.Parameters.AddWithValue("@Absent", vAbsent)
            cmd.Parameters.AddWithValue("@Present", vPresent)
            cmd.Parameters.AddWithValue("@EQ", vEQ)

        Next

        myconnection.Close()
        MessageBox.Show("Saved Successfully!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Catch ex As OleDb.OleDbException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "oledb Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try
End Sub

这是我们用来保存数据库中已选/未选中复选框的代码。不幸的是,它没有工作!但是当我们使用这个程序的其他部分,它工作!所以有点混乱为什么它不工作的程序部分我们放这个代码。谁能帮助我们?非常感谢!

This is the code we use to save the checked/unchecked checkboxes in the database. Unfortunately, it didn't work! but when we used this to the other part of the program it worked! so were a little bit confuse on why it didn't work on the part of the program where we put this code . can anyone help us? thank you very much!

推荐答案

Private Sub btnSave_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles btnSave 。单击
尝试
myconnection.Open()
变量声明
Dim str As String

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Try myconnection.Open() 'Declaration of Variables Dim str As String

    Dim vCourseCode As String
    Dim vSection As String
    Dim vSurname As String
    Dim vFirstName As String
    Dim vMiddleInitial As String
    Dim vDate1 As Boolean
    Dim vDate2 As Boolean
    Dim vDate3 As Boolean
    Dim vDate4 As Boolean
    Dim vDate5 As Boolean
    Dim vDate6 As Boolean
    Dim vDate7 As Boolean
    Dim vDate8 As Boolean
    Dim vDate9 As Boolean
    Dim vAbsent As String
    Dim vPresent As String
    Dim vEQ As String


    For x As Integer = 0 To DataGridView1.Rows.Count - 2
        vCourseCode = DataGridView1.Rows(x).Cells(1).Value
        vSection = DataGridView1.Rows(x).Cells(2).Value
        vSurname = DataGridView1.Rows(x).Cells(3).Value
        vFirstName = DataGridView1.Rows(x).Cells(4).Value
        vMiddleInitial = DataGridView1.Rows(x).Cells(5).Value 
        vAbsent = DataGridView1.Rows(x).Cells(6).Value
        vPresent = DataGridView1.Rows(x).Cells(7).Value
        vEQ = DataGridView1.Rows(x).Cells(8).Value
        vDate1 = DataGridView1.Rows(x).Cells(0).Value
        vDate2 = DataGridView1.Rows(x).Cells(1).Value
        vDate3 = DataGridView1.Rows(x).Cells(2).Value
        vDate4 = DataGridView1.Rows(x).Cells(3).Value
        vDate5 = DataGridView1.Rows(x).Cells(4).Value
        vDate6 = DataGridView1.Rows(x).Cells(5).Value
        vDate7 = DataGridView1.Rows(x).Cells(6).Value
        vDate8 = DataGridView1.Rows(x).Cells(7).Value
        vDate9 = DataGridView1.Rows(x).Cells(8).Value

        str = "Insert into StudentsAttendance ([CourseCode],[Section],[Surname],[FirstName],[MiddleName],[Date1],[Date2],[Date3],[Date4],[Date5],[Date6],[Date7],[Date8],[Date9],[Absent],[Present],[EQ]) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"


        Dim cmd As OleDbCommand = New OleDbCommand(str, myconnection)
        cmd.Parameters.AddWithValue("@CourseCode", vCourseCode)
        cmd.Parameters.AddWithValue("@Section", vSection)
        cmd.Parameters.AddWithValue("@Surname", vSurname)
        cmd.Parameters.AddWithValue("@FirstName", vFirstName)
        cmd.Parameters.AddWithValue("@MiddleName", vMiddleInitial)
        cmd.Parameters.AddWithValue("@Date1", vDate1)
        cmd.Parameters.AddWithValue("@Date2", vDate2)
        cmd.Parameters.AddWithValue("@Date3", vDate3)
        cmd.Parameters.AddWithValue("@Date4", vDate4)
        cmd.Parameters.AddWithValue("@Date5", vDate5)
        cmd.Parameters.AddWithValue("@Date6", vDate6)
        cmd.Parameters.AddWithValue("@Date7", vDate7)
        cmd.Parameters.AddWithValue("@Date8", vDate8)
        cmd.Parameters.AddWithValue("@Date9", vDate9)
        cmd.Parameters.AddWithValue("@Absent", vAbsent)
        cmd.Parameters.AddWithValue("@Present", vPresent)
        cmd.Parameters.AddWithValue("@EQ", vEQ)
        '==========PUT A cmd EXECUTION here so all your data will insert==========
        cmd.ExecuteNonQuery()
        '=========================================================================
    Next

    myconnection.Close()
    MessageBox.Show("Saved Successfully!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As OleDb.OleDbException
    MsgBox(ex.Message, MsgBoxStyle.Critical, "oledb Error")
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub

尝试这个我认为缺少执行...

Try this i think that is missing to execute...

这篇关于如何保存检查和取消选中复选框从datagridview到数据库使用vb.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆