当我尝试使用代码更新GridViewBox以更新数据库时,会收到以下错误消息
对象引用未设置为对象的实例
但我不太明白。如果能帮上忙,我将不胜感激。

Imports MySql.Data.MySqlClient
Public Class Form1

    //GLOBAL DELARATION
    Dim conString As String = "Server=xxx;" & "Database=xxx;" & "Uid=xxx;" & "xxx;"
    Dim con As New MySqlConnection(conString)
    Dim cmd As MySqlCommand
    Dim adapter As MySqlDataAdapter

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        //TODO: This line of code loads data into the 'OlejnaDataSet.people' table. You can move, or remove it, as needed.
        Me.PeopleTableAdapter.Fill(Me.OlejnaDataSet.people)
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

    End Sub

    Private Sub hent()
        Me.PeopleTableAdapter.Fill(Me.OlejnaDataSet.people)
    End Sub

    Private Sub cleartxt()
        TextBox_name.Text = ""
        TextBox_position.Text = ""
        TextBox_team.Text = ""

    End Sub

    Private Sub Add()
        Dim sql As String = "INSERT INTO people(Name,Position,Team) VALUES(@NAME,@POSITION,@TEAM)"
        cmd = New MySqlCommand(sql, con)

        //PARAMETERS
        cmd.Parameters.AddWithValue("@NAME", TextBox_name.Text)
        cmd.Parameters.AddWithValue("@POSITION", TextBox_position.Text)
        cmd.Parameters.AddWithValue("@TEAM", TextBox_team.Text)

        //Open Connection and INSERT
        Try
            con.Open()
            If cmd.ExecuteNonQuery() > 0 Then
                MsgBox("Added")
                cleartxt()
            End If
            con.Close()
            hent()
        Catch ex As Exception

            MsgBox(ex.Message)
            con.Close()
        End Try
    End Sub

    //UPDATE DATABASE
    Private Sub UpdateDG(id As String)
        Dim sql As String = "UPDATE people SET name='" + TextBox_name.Text + "', position='" + TextBox_position.Text + "', team='" + TextBox_team.Text + "' WHERE ID='" + id + "'"

        //Open CON, EXECUTE UPDATE, CLOSE

        Try
            con.Open()
            adapter.UpdateCommand = con.CreateCommand()
            adapter.UpdateCommand.CommandText = sql

            If adapter.UpdateCommand.ExecuteNonQuery() > 0 Then
                MsgBox("You have now updated")
                cleartxt()
            End If
            con.Close()
            //REFRESH
            hent()

        Catch ex As Exception
            MsgBox(ex.Message)
            con.Close()

        End Try
    End Sub

    Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
        Add()

    End Sub

    Private Sub ButtonDelete_Click(sender As Object, e As EventArgs) Handles ButtonDelete.Click

    End Sub

    Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click
        Dim id As String = DataGridView1.SelectedRows(0).Cells(0).Value
        UpdateDG(id)
    End Sub

    Private Sub DataGridView1_MouseClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseClick
        Dim name As String = DataGridView1.SelectedRows(0).Cells(1).Value
        Dim position As String = DataGridView1.SelectedRows(0).Cells(2).Value
        Dim team As String = DataGridView1.SelectedRows(0).Cells(3).Value

        TextBox_name.Text = name
        TextBox_position.Text = position
        TextBox_team.Text = team
    End Sub
End Class

mysql - 将Gridview列编辑到MySQL-LMLPHP

最佳答案

希望您已单击Updatebutton而未在您的DataGrid中选择任何行
按如下所示更改Update按钮Click事件:

 Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click
     If DataGridView1.SelectedRows.Count > 0 Then
        Dim id As String = DataGridView1.SelectedRows(0).Cells(0).Value
        UpdateDG(id)
     End If
 End Sub

更新
请声明并初始化您的Sql Adapter
使用如下:
SqlDataAdapter adapter = new SqlDataAdapter();
    //UPDATE DATABASE
    Private Sub UpdateDG(id As String)
        Dim sql As String = "UPDATE people SET name='" + TextBox_name.Text + "', position='" + TextBox_position.Text + "', team='" + TextBox_team.Text + "' WHERE ID='" + id + "'"

        //Open CON, EXECUTE UPDATE, CLOSE

        Try
            SqlDataAdapter adapter = new SqlDataAdapter();
            con.Open()
            adapter.UpdateCommand = con.CreateCommand()
            adapter.UpdateCommand.CommandText = sql
            If adapter.UpdateCommand.ExecuteNonQuery() > 0 Then
                MsgBox("You have now updated")
                cleartxt()
            End If
            con.Close()
            //REFRESH
            hent()
        Catch ex As Exception
            MsgBox(ex.Message)
            con.Close()
        End Try
    End Sub

10-07 22:47