1

好的,所以我在 vb.net 应用程序中添加了一个 Ms 访问表,然后我创建了一个过滤器,该表包含带有 DBNull 值的列,问题是每当我开始在过滤器中写入任何具有 dbnull 值的单元格的内容时,它都会让我知道错误

""" 表 'Parts' 中的列 'Postion' 的值为 DBNull。"""

并且异常详细信息是 """""""""""""""""

System.Data.StrongTypingException was unhandled by user code
  Message=The value for column 'Postion' in table 'Parts' is DBNull.
  Source=Erkat
  StackTrace:
       at Erkat.cutterprogDataSet.PartsRow.get_Postion() in C:\Users\Mina\Documents\Visual Studio 2010\Projects\Erkatpj\Erkat\cutterprogDataSet.Designer.vb:line 2634
  InnerException: System.InvalidCastException
       Message=Conversion from type 'DBNull' to type 'String' is not valid.
       Source=Microsoft.VisualBasic
       StackTrace:
            at Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value)
            at Erkat.cutterprogDataSet.PartsRow.get_Postion() in C:\Users\Mina\Documents\Visual Studio 2010\Projects\Erkatpj\Erkat\cutterprogDataSet.Designer.vb:line 2632
       InnerException: 

"""""""""""""""""""

我找到了一个解决方案,但它只是暂时的

在设计器自动生成的代码中

"""

   <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
    Public Property Postion() As String
        Get
            Try 
                Return CType(Me(Me.tableParts.PostionColumn),String)
            Catch e As Global.System.InvalidCastException
                Throw New Global.System.Data.StrongTypingException("The value for column 'Postion' in table 'Parts' is DBNull.", e)
            End Try
        End Get
        Set
            Me(Me.tableParts.PostionColumn) = value
        End Set
    End Property

""" 我把它改成了

""""""""""""""""""""""

<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
         Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
        Public Property Postion() As String
            Get
                Try
                    If Convert.IsDBNull(Me(Me.tableParts.PostionColumn)) Then
                        Return Nothing
                    Else
                        Return CType(Me(Me.tableParts.PostionColumn), String)

                    End If

                Catch e As Global.System.InvalidCastException
                    Throw New Global.System.Data.StrongTypingException("The value for column 'Postion' in table 'Parts' is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableParts.PostionColumn) = value
            End Set
        End Property 

""""""""""""""""""""""" 但是由于它的自动生成它不断删除它并再次制作旧的

有人可以帮忙吗?

4

1 回答 1

0

存储Public Property Postion() As String为 anObject并强制转换为Stringif Positionis not DBNull.Valueor Nothing

Public Property Postion() As Object
    Get
        Return Me(Me.tableParts.PostionColumn
    End Get
    Set
        Me(Me.tableParts.PostionColumn) = value
    End Set
End Property

用法:

Dim pos as String
Dim obj as Object = Position
If Not (obj Is Nothing) And Not (obj = DBNull.Value) Then
  pos = CType(obj, String)
  ' Alternately:
  ' pos = CString(obj)
  ' Or:
  ' pos = obj.ToString()
Else
  pos = String.Empty
End If

我的VB不太好,如有语法问题请见谅。

于 2012-08-28T21:17:33.037 回答