错误dapper值不能为空与数据库ms访问vb.net

ibrsph3r  于 2023-05-30  发布在  .NET
关注(0)|答案(1)|浏览(144)

我试图通过按钮referesh,然后我删除了网格,然后一个错误出现,即使我已经使grid.ondelete假。有没有什么解决办法可以避免这个错误
谢谢

Public Class Form1
    Private uService As New UserService()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Grid.ReadOnly = True
        Grid.OnDelete(Of User)(Function(r, row)
                                   Return False
                               End Function)
        LoadData()
    End Sub
    Private Sub LoadData()
        Dim user = uService.GetUser()
        Grid.Bind(user)
    End Sub
    Private Sub btnrefresh_Click(sender As Object, e As EventArgs) Handles btnrefresh.Click
        LoadData()
    End Sub
End Class
Public Module DbContext
    Public Function GetOledbConnectionString() As String
        Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
    End Function
End Module
Public Class UserService
    Private ReadOnly _conn As OleDbConnection
    Private _connectionString As String = DbContext.GetOledbConnectionString()
    Public Sub New()
        _conn = New OleDbConnection(_connectionString)
    End Sub
    Public Function GetUser() As IEnumerable(Of User)
        Dim sql = "SELECT * FROM Users"
        Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
            Return _conn.Query(Of User)(sql).ToList()
        End Using
    End Function
End Class
End Class

'result from diagnostic tools This is a copy paste from visual studio
Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Value cannot be null.
Parameter name: key</Message><StackTrace>   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at    at Kimtoo.BindingProvider.DGVBindingProvider.&amp;lt;&amp;gt;c__DisplayClass29_0`1.&amp;lt;Bind&amp;gt;b__3(Object s, DataGridViewRowCancelEventArgs e)
9lowa7mx

9lowa7mx1#

所以我的解决方案是阻止或关闭来自绑定提供者的onDelete事件,即键盘Delete按钮与DataGridView按键

Private Sub Grid_KeyDown(sender As Object, e As KeyEventArgs) Handles Grid.KeyDown
        Try
    If e.KeyCode = Keys.Delete AndAlso e.KeyValue = 46 Then ' Delete Key
    e.Handled = True
    End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

相关问题