对象不能从DBNULL转换为ther类型 [英] object cannot be cast from DBNULL to ther types

查看:322
本文介绍了对象不能从DBNULL转换为ther类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用GetOrdinal。代码

I'am using GetOrdinal in my code. the code

mastxtDateEnded_Edit.Text =
        Convert.ToDateTime(cursor.GetValue(cursor.GetOrdinal("DateEnded")))
               .ToString("MM/dd/yyyy"));

Error: object cannot be cast from DBNULL to ther types.   


推荐答案

您可以使用 IsDBNull method 检查空值:

You can use the IsDBNull method to check for the null value:

int index = cursor.GetOrdinal("DateEnded")
mastxtDateEnded_Edit.Text =
  cursor.IsDBNull(index) ?
    String.Empty :
    ((DateTime)(cursor.GetValue(index))).ToString("MM/dd/yyyy");

这篇关于对象不能从DBNULL转换为ther类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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