【问题标题】:Typed DataSet - InvalidCastException类型化数据集 - InvalidCastException
【发布时间】:2011-11-15 08:03:58
【问题描述】:

我有一个类型化的数据集。我将具有绑定源的数据绑定到具有 200 个文本框、数据编辑器等的表单。一切正常,只有一个例外:

在我的数据库中,我有空的 DateTimes (DBNull.Value)。当它们绑定到 DateEdit-Control 时,我得到以下异常:

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.Data.StrongTypingException' occurred in myTestDLL.dll

我尝试将 DataSet-Desinger 中的 NullValue-Property 更改为“引发异常”,但它不适用于 DateTime。对于其他类型,如 Integer 或 String 它工作正常。

我不知道一个好的解决方案(现在我用一些虚拟日期填充空日期并使其在 DateEdit-Control 中不可见,但这很不酷),我希望在这里找到一些帮助。

似乎很多人都遇到过同样的问题,但我没有找到解决方案:(

【问题讨论】:

    标签: vb.net datetime dataset .net typed


    【解决方案1】:

    如果测试失败,为什么不跳过?

    if dMyDate is dbnull.value then 'skip, msgbox, set to date.minValue, whatever
    

    【讨论】:

    • 感谢您的回答!但是在什么时候我应该检查一下?如果控件获得未绑定的值,您的解决方案将起作用,但我看不到它背后有绑定源的方法。还有其他想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2012-07-06
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多