当我尝试将代码更新public override ObservableCollection<object> Collection { get; set; }
以更新到我的数据库时,我收到以下错误消息
对象引用未设置为对象的实例
但我无法弄明白。非常感谢我能得到的任何帮助。
GridViewBox
答案 0 :(得分:0)
希望您点击Update
button
而不选择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