vb 2010,删除datagridview中的行以更新访问表

时间:2012-08-04 23:32:58

标签: vb.net ms-access datagridview oledb

我已在表格中声明这些是私人的。

Private callLogConnection As New OleDbConnection()
Private schDataAdapter = New OleDbDataAdapter("Select * From tbl_schtime", _ callLogConnection)
Private schCommmandBuilder = New OleDbCommandBuilder(schDataAdapter)
Private schDataTable As New DataTable
Private schRowPosition As Integer = 0
Private qryexceptionDataAdapter = New OleDbDataAdapter("Select * From  _ qry_exceptionUpdate", callLogConnection)
Private exceptionBindingSource = New BindingSource()
Private exception2BindingSource As New BindingSource
Private exceptionCommandBuilder As OleDbCommandBuilder = New _ OleDbCommandBuilder(qryexceptionDataAdapter)
Private qryexceptionDataTable As New DataTable
Private qryexceptionRowPostiion As Integer = 0
Private tbl_ExceptionDataSet As DataSet = New DataSet
Private exceptionDataTable As New DataTable

我已在表单load

中声明了这些对象
qryexceptionDataAdapter.Fill(qryexceptionDataTable)
'linking the qryexception table to binding source
exceptionBindingSource.DataSource = qryexceptionDataTable
'showing the binding source in the datagrid view
dgvExceptions.DataSource = exceptionBindingSource

这是我的保存按钮命令,用于从datagridview更新我的表。

Private Sub btnSaveException_Click(sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveException.Click
    Try
        Me.Validate()
        Me.qryexceptionDataAdapter.Update(Me.tbl_ExceptionDataSet.Tables("qry_exceptionUpdates"))
        Me.tbl_ExceptionDataSet.AcceptChanges()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    End Sub code here

但我一直在接受:

System.Reflection.AmbiguousMatchException: Overload resolution failed because no Public 'Update' is most specific for these arguments:
    'Public Function Update(dataTable As System.Data.DataTable) As Integer':
        Not most specific.
    'Public Overrides Function Update(dataSet As System.Data.DataSet) As Integer':
        Not most specific.
    'Public Function Update(dataRows As System.Data.DataRow()) As Integer':
        Not most specific.
   at Microsoft.VisualBasic.CompilerServices.OverloadResolution.ResolveOverloadedCall(String MethodName, List`1 Candidates, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, BindingFlags LookupFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.OverloadResolution.ResolveOverloadedCall(String MethodName, MemberInfo[] Members, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, BindingFlags LookupFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ResolveCall(Container BaseReference, String MethodName, MemberInfo[] Members, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, BindingFlags LookupFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   at Call_Log.CallLogForm.btnSaveException_Click(Object sender, EventArgs e) in C:\Users\mdutton\Desktop\Call Log\Call Log\CallLogForm.vb:line 174

1 个答案:

答案 0 :(得分:1)

线索在你的调用堆栈中:

Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall

这意味着VB试图找出它应该在运行时调用哪个重载而不是设计时。被抛出的异常最有可能意味着:

Me.tbl_ExceptionDataSet.Tables("qry_exceptionUpdates")

评估为Nothing,因为有3个重载采用了可能与您的请求匹配的单个对象参数(为您帮助列出的异常)。

所以,这里有两件事需要解决:

1)在项目属性中设置Option Strict On。来自Microsoft Documentation

  

除了禁止隐式缩小转化之外,还有选项   严格为后期绑定生成错误。对象是后期绑定的   当它被赋值给声明为类型的变量时   对象

     

因为Option Strict On提供强类型,可以防止意外   输入数据丢失的转换,禁止后期绑定,并改进   性能,强烈建议使用它。

2)完成上述操作后,仍然会出现运行时错误,但现在DataTable参数不能为null(Nothing),因此您也需要修复它。

您的示例代码中可能有拼写错误,但按钮点击事件中的表名与您在select语句中的表名不匹配(qry_exceptionUpdates vs _qry_exceptionUpdate)。

如果可能,您应该在全局或模块级常量中指定一次表名,这样就不会遇到这种类型的命名问题。