将Gridview列编辑到MySQL
当我尝试使用代码更新将Gridview列编辑到MySQL,mysql,vb.net,datagridview,Mysql,Vb.net,Datagridview,当我尝试使用代码更新GridViewBox以更新数据库时,我收到以下错误消息 对象引用未设置为对象的实例 但我不太明白。如果我能得到任何帮助,我将不胜感激 Imports MySql.Data.MySqlClient Public Class Form1 //GLOBAL DELARATION Dim conString As String = "Server=xxx;" & "Database=xxx;" & "Uid=xxx;" & "xxx;"
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
希望您单击了
更新按钮
,而没有选择数据网格中的任何行
更改更新
按钮单击
事件,如下所示:
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适配器
使用方法如下:
SqlDataAdapter=newsqldataadapter()代码>
在哪一行中,您得到了错误?我直接从GridView中得到了错误-表单仍在运行:感谢您的输入,但我仍然有同样的问题:调试代码并找出导致错误的行我在第60行得到错误:DataGridView.exe中发生了类型为“System.NullReferenceException”的未处理异常附加信息:对象引用未设置为对象的实例。
//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