英文:
error dapper Value cannot be null with database ms access in vb.net
问题
I tried to referesh via the button then I deleted the grid then an error appeared even though I had made grid.ondelete false. Is there any solution so I can avoid the error
' 译文:我尝试通过按钮刷新,然后删除了表格,然后出现了错误,尽管我已将grid.ondelete设置为false。是否有解决方案以避免这个错误
Thanks
' 译文:谢谢
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
' 译文:来自诊断工具的结果,这是从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)
<details>
<summary>英文:</summary>
I tried to referesh via the button then I deleted the grid then an error appeared even though I had made grid.ondelete false. Is there any solution so I can avoid the error
Thanks
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
[![Value cannot be null][1]][1]
'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.Dictionary2.FindEntry(TKey key)
2.get_Item(TKey key)
at System.Collections.Generic.Dictionary
at at Kimtoo.BindingProvider.DGVBindingProvider.&lt;&gt;c__DisplayClass29_0`1.&lt;Bind&gt;b__3(Object s, DataGridViewRowCancelEventArgs e)
[1]: https://i.stack.imgur.com/jJ57u.jpg
</details>
# 答案1
**得分**: 1
我的解决方案是阻止或关闭绑定提供程序的ondelete事件,即在datagridview keypress中与键盘删除按钮相关的事件。
```vb
Private Sub Grid_KeyDown(sender As Object, e As KeyEventArgs) Handles Grid.KeyDown
Try
If e.KeyCode = Keys.Delete AndAlso e.KeyValue = 46 Then ' 删除键
e.Handled = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
英文:
So my solution is to prevent or turn off the ondelete event from the binding provider, namely the keyboard delete button with the datagridview keypress
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论