IsDBNull(字段)无法正常工作。 [英] IsDBNull (field) doesn't work correctly.

查看:317
本文介绍了IsDBNull(字段)无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IsDBNull(字段)检查DBNull值的字段,但该语句永远不会被执行。当我按F11在Debug中执行该行时,它会立即跳转到DataSet.Designer.vb中的代码,并在catch e语句后停止在field属性语句中(参见下面的代码)。

 公共 属性 T3d()正如 字符串 
获取
尝试
返回 CType Me .tableStudentSchedule.T3dColumn), String
Catch e As Global .System。 InvalidCastException
投掷 全局 .System.Data.StrongTypingException( 列的值'表'StudentSchedule'中的T3d'是DBNull。,e)
结束 尝试
结束 获取
设置
.tableStudentSchedule.T3dColumn)= value
结束 设置
结束 属性



e.message是从类型'DBNull'转换为'String'类型无效。

由于这是自动生成的代码,我没有改变它。



我试过检查字段为dbnull几种不同的方式,包括使用g以下功能。

 公共 功能 NotNull( Of  T)( ByVal  As  T, ByVal  DefaultValue  As  T) As  T 
如果 什么 OrElse IsDBNull(Value)然后
返回 DefaultValue
否则
返回
结束 如果
结束 < span class =code-keyword>功能



永远不会到达函数,因为它直接进入DataSet.Designer.vb并在catch e语句后停在field属性



这是包含所有IsDBNull检查的代码在相同的代码中。它们都不像我期望的那样工作。

   
Dim schDateRow As TmsDatabaseDataSet.StudentScheduleRow = TmsDatabaseDataSet.StudentSchedule.FindBySchDate(DtCurrent)
Dim pschDateRow As TmsDatabaseDataSet.StudentScheduleRow = TmsDatabaseDataSet.StudentSchedule.FindBySchDate(DtPrevious)
schDateRow.Hlights = NotNull((pschDateRow。 Hlights),
schDateRow.T1 = NotNull(pschDateRow.T1,
schDateRow.T2 = NotNull(pschDateRow.T2,
schDateRow.T2d = NotNull(pschDateRow.T2d,
schDateRow。 T3 = NotNull(pschDateRow.T3,

' 从不执行该功能。
' 直接转到数据集设计器字段属性
schDateRow.T3d = NotNull(pschDateRow.T3d, String .Empty)

' 从不执行if语句
如果 IsDBNull(pschDateRow.T3d)那么
schDateRow.T3d = String .Empty
Else
schDateRow.T3d = pschDateRow.T3d
结束 如果

' 与上述相同
如果 DBNull.Value.Equals(pschDateRow.T3d)然后
schDateRow.T3d = pschDateRow.T3d
其他
schDateRow.T3d = 字符串 .Empty
结束 如果

' 如上所述,在以下每一行死亡
schDateRow。 T3d = 如果 DBNull.Value.Equals(pschDateRow.T3d), String .Empty,pschDateRow.T3d)
schDateRow.T3d = 如果(IsDBNull(pschDateRow.T3d),字符串 .Empty,pschDateRow.T3d)

DtCurrent = DtPrevious
DtPrevious = DateAdd(DateInterval.Day,-7,DtPrevious)
循环 DtPrevious> = DtFrom



我搜索了我能找到的每篇文章,找出它为什么不起作用。我在使用它之前检查字段是否为DBNull,但是它永远不会通过IsDBNull(字段)检查。



顺便说一下,我正在使用Visual Studio 2013 ,自2012年以来一直崩溃。



还有一件事。此数据填充datagridview,包括具有DBNull的记录,因此它使用DBNull值正确填充DataGridview。我想在DataGRidView中进行更改,但这应该工作。



感谢您的帮助。

解决方案

发现它!!!!!



在解决DBNull.value字段时,如上面的代码所示,我用了

 如果 pschDateRow.IsT3dNull 那么 
schDateRow.T3d = 字符串 .Empty
其他
schDateRow.T3d = pschDateRow.T3d
结束 如果



这完美运作。偶然发现它。显然,代码生成器生成一个标记为'Is'+'field name'+'Null'的DBNull检查,可用于直接检查字段而不会导致DataSet.Designer.vb阻塞它。



感谢所有建议。


check

Me.tableStudentSchedule.T3IsNull



然后尝试



 公共 < span class =code-keyword>函数 NotNull( ByVal 作为 对象 ByVal  DefaultValue  As  T) As  T 
如果 Nothing OrElse DBNull.Value 然后
返回 DefaultValue
其他
返回
结束 if
结束 功能


I am checking a field for DBNull value using IsDBNull(field), but the statement never gets executed. When I press F11 to execute the line in Debug, it immediately jumps to the code in the DataSet.Designer.vb and stops at the field property statement after the catch e statement (see code below ).

Public Property T3d() As String
    Get
        Try
            Return CType(Me(Me.tableStudentSchedule.T3dColumn),String)
        Catch e As Global.System.InvalidCastException
            Throw New Global.System.Data.StrongTypingException("The value for column 'T3d' in table 'StudentSchedule' is DBNull.", e)
        End Try
    End Get
    Set
        Me(Me.tableStudentSchedule.T3dColumn) = value
    End Set
End Property


The e.message is "Conversion from type 'DBNull' to type 'String' is not valid."
Since this is auto-generated code, I haven't changed it.

I've tried checking the field for dbnull several different ways including using the following function.

Public Function NotNull(Of T)(ByVal Value As T, ByVal DefaultValue As T) As T
   If Value Is Nothing OrElse IsDBNull(Value) Then
     Return DefaultValue
   Else
     Return Value
   End if
End Function


It never gets to the function because it goes directly to the DataSet.Designer.vb and stops at the field property after the catch e statement

This is the code with all of the IsDBNull checks in the same code. None of them work the way I expect them to work.

Do
  Dim schDateRow As TmsDatabaseDataSet.StudentScheduleRow = TmsDatabaseDataSet.StudentSchedule.FindBySchDate(DtCurrent)
  Dim pschDateRow As TmsDatabaseDataSet.StudentScheduleRow = TmsDatabaseDataSet.StudentSchedule.FindBySchDate(DtPrevious)
  schDateRow.Hlights = NotNull((pschDateRow.Hlights), "")
  schDateRow.T1 = NotNull(pschDateRow.T1, "")
  schDateRow.T2 = NotNull(pschDateRow.T2, "")
  schDateRow.T2d = NotNull(pschDateRow.T2d, "")
  schDateRow.T3 = NotNull(pschDateRow.T3, "")

  'Never executes the function.
  'Goes directly to the dataset designer field property
  schDateRow.T3d = NotNull(pschDateRow.T3d, String.Empty)

  'Never executes the if statement
  If IsDBNull(pschDateRow.T3d) Then
     schDateRow.T3d = String.Empty
  Else
     schDateRow.T3d = pschDateRow.T3d
  End If

  'Same as above
  If Not DBNull.Value.Equals(pschDateRow.T3d) Then
     schDateRow.T3d = pschDateRow.T3d
  Else
     schDateRow.T3d = String.Empty
  End If

  'Dies at each of the following lines as above
  schDateRow.T3d = If(Not DBNull.Value.Equals(pschDateRow.T3d), String.Empty, pschDateRow.T3d)
  schDateRow.T3d = If(IsDBNull(pschDateRow.T3d), String.Empty, pschDateRow.T3d)

   DtCurrent = DtPrevious
   DtPrevious = DateAdd(DateInterval.Day, -7, DtPrevious)
Loop While DtPrevious >= DtFrom


I've searched every article I can find to find out why it doesn't work. I check the field for DBNull before using it, but it never gets past the the IsDBNull(field) check.

By the way, I'm using Visual Studio 2013, since 2012 crashes all the time.

One more thing. This data fills a datagridview, including the record with the DBNull, so it fills the DataGridview properly with the DBNull value. I thought of doing the change in the DataGRidView, but this SHOULD work.

Thanks for any help.

解决方案

FOUND IT!!!!!

When addressing the field which is DBNull.value, as in the above code, I used

If pschDateRow.IsT3dNull Then
    schDateRow.T3d = String.Empty
Else
    schDateRow.T3d = pschDateRow.T3d
End If


This worked perfectly. Found it by accident. Apparently the code generator generates a DBNull check labeled 'Is' + 'field name' + 'Null', which can be used to check the field directly without causing the DataSet.Designer.vb to choke on it.

Thanks for all of the suggestions.


check
Me.tableStudentSchedule.T3IsNull

then try

Public Function NotNull(ByVal Value As Object, ByVal DefaultValue As T) As T
   If Value Is Nothing OrElse Value is DBNull.Value Then
     Return DefaultValue
   Else
     Return Value
   End if
End Function


这篇关于IsDBNull(字段)无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆