Обновление базы данных MS Access через DataGridView в Visual Basic - PullRequest
0 голосов
/ 04 ноября 2018

Я новичок в vb.net и пытаюсь обновить свою базу данных MS Access из таблицы данных в Visual Basic с помощью кнопки. Всякий раз, когда я нажимаю на кнопку для обновления, я получаю сообщение об ошибке «NullReferenceException не обработано». Пожалуйста, помогите мне. Вот мои коды:

Imports System.Data.OleDb

Public Class Form1
    Public Cnn As New OleDb.OleDbConnection
    Public Cmd As New OleDb.OleDbCommand
    Public DR As OleDbDataReader
    Public DA As OleDbDataAdapter
    Public DS As DataSet
    Public DT As DataTable
    Public cmdBld As OleDbCommandBuilder

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DTBClassRecordsDataSet.tblRecords' table. You can move, or remove it, as needed.
        Me.TblRecordsTableAdapter.Fill(Me.DTBClassRecordsDataSet.tblRecords)

        dgvRecords.AllowUserToAddRows = True
        dgvRecords.AutoGenerateColumns = True



        If Cnn.State = ConnectionState.Open Then Cnn.Close()
        Cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\JEM FLOR\Projects\CLASS RECORD\DTBClassRecords.accdb"
        Cnn.Open()

        Cmd.Connection = Cnn


        ds.Tables.Add(dt)
        DA = New OleDbDataAdapter("Select * from tblRecords", Cnn)
        Dim cb = New OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"
        da.Fill(dt)
        dgvRecords.DataSource = dt.DefaultView



        DataGridShow()

    End Sub

    Private Sub DataGridShow()
        Cmd = New OleDbCommand("SELECT * FROM tblRecords", Cnn)

        Cnn.Open()

        DA = New OleDbDataAdapter(Cmd)

        cmdBld = New OleDbCommandBuilder(DA)

        DS = New DataSet()

        DA.Fill(DS, "table1")

        dgvRecords.DataSource = DS.Tables("tblRecords").DefaultView

        Cnn.Close()

    End Sub

    Private Sub dgvRecords_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvRecords.CellEndEdit
        For Each row As DataGridViewRow In dgvRecords.Rows
            On Error Resume Next

            If Not row.IsNewRow Then

                row.Cells("QR1DataGridViewTextBoxColumn").Value = ((row.Cells("Q1DataGridViewTextBoxColumn").Value / row.Cells("TI1DataGridViewTextBoxColumn").Value) * 50 + 50)
                row.Cells("QR2DataGridViewTextBoxColumn").Value = ((row.Cells("Q2DataGridViewTextBoxColumn").Value / row.Cells("TI2DataGridViewTextBoxColumn").Value) * 50 + 50)
                row.Cells("QR3DataGridViewTextBoxColumn").Value = ((row.Cells("Q3DataGridViewTextBoxColumn").Value / row.Cells("TI3DataGridViewTextBoxColumn").Value) * 50 + 50)
                row.Cells("QR4DataGridViewTextBoxColumn").Value = ((row.Cells("Q4DataGridViewTextBoxColumn").Value / row.Cells("TI4DataGridViewTextBoxColumn").Value) * 50 + 50)
                row.Cells("QR5DataGridViewTextBoxColumn").Value = ((row.Cells("Q5DataGridViewTextBoxColumn").Value / row.Cells("TI5DataGridViewTextBoxColumn").Value) * 50 + 50)
            End If

            Dim qr1 As Integer = row.Cells("QR1DataGridViewTextBoxColumn").Value.ToString
            Dim qr2 As Integer = row.Cells("QR2DataGridViewTextBoxColumn").Value.ToString
            Dim qr3 As Integer = row.Cells("QR3DataGridViewTextBoxColumn").Value.ToString
            Dim qr4 As Integer = row.Cells("QR4DataGridViewTextBoxColumn").Value.ToString
            Dim qr5 As Integer = row.Cells("QR5DataGridViewTextBoxColumn").Value.ToString


            If Not row.IsNewRow Then
                Dim rating As Double = CInt((qr1 + qr2 + qr3 + qr4 + qr5) / 5)
                row.Cells("QRDataGridViewTextBoxColumn").Value = rating
            End If


        Next
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim sql As String = "INSERT INTO tblRecords(ID,ID_NO,NAME,TI1,Q1,QR1,TI2,Q2,QR2,TI3,Q3,QR3,TI4,Q4,QR4,TI5,Q5,QR5,QR) VALUES(@IDDataGridViewTextBoxColumn,@IDNODataGridViewTextBoxColumn,@NAMEDataGridViewTextBoxColumn,@TI1DataGridViewTextBoxColumn,@Q1DataGridViewTextBoxColumn,@QR1DataGridViewTextBoxColumn,@TI2DataGridViewTextBoxColumn,@Q2DataGridViewTextBoxColumn,@QR2DataGridViewTextBoxColumn,@TI3DataGridViewTextBoxColumn,@Q3DataGridViewTextBoxColumn,@QR3DataGridViewTextBoxColumn,@TI4DataGridViewTextBoxColumn,@Q4DataGridViewTextBoxColumn,@QR4DataGridViewTextBoxColumn,@TI5DataGridViewTextBoxColumn,@Q5DataGridViewTextBoxColumn,@QR5DataGridViewTextBoxColumn,@QRDataGridViewTextBoxColumn)"

        Cmd = New OleDbCommand(sql, Cnn)

        For Each row As DataGridViewRow In dgvRecords.Rows
            'ADD Parameters
            Cmd.Parameters.AddWithValue("@ID", row.Cells("IDDataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("IDNODataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("NAMEDataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("TI1DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("Q1DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("QR1DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("TI2DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("Q2DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("QR2DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("TI3DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("Q3DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("QR3DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("TI4DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("Q4DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("QR4DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("TI5DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("Q5DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("QR5DataGridViewTextBoxColumn").Value)
            Cmd.Parameters.AddWithValue("@ID", row.Cells("QRDataGridViewTextBoxColumn").Value)
        Next


        Try
            Cnn.Open()

            If Cmd.ExecuteNonQuery() > 0 Then
                MsgBox("Successfully saved.")
            End If
            Cnn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            Cnn.Close()
        End Try
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Me.Validate()

        Me.da.Update(Me.ds.Tables("tblRecords"))

        Me.ds.AcceptChanges()
    End Sub
End Class
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...