从类型“为DBNull”转换为类型“string”无效 [英] Conversion from type 'DBNull' to type 'String' is not valid

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

问题描述

我收到这个问题。

从类型为DBNull转换为类型string是无效的。

Conversion from type 'DBNull' to type 'String' is not valid.

501线:hfSupEmail.Value =
  dt.Rows(0)(SupEmail)

Line 501: hfSupEmail.Value = dt.Rows(0)("SupEmail")

我很新的这一点,我真的不知道是什么确切的问题
可能有人指导我?

i am very new to this, i am not really sure what is the exact problem could someone guide me?

非常感谢

推荐答案

的快速和肮脏的修复:

hfSupEmail.Value = dt.Rows(0)("SupEmail").ToString()

这工作得很好,当你最终的目标和源数据已经是字符串。这是因为任何多余的的ToString()调用的东西,已经是一个字符串,通常会被抖动成无操作进行优化,如果它是NULL,则所得 DBNull.Value.ToString()前pression产生你想要的空字符串。

This works very well when your eventual target and the source data are already strings. This is because any extra .ToString() call for something that's already a string will generally be optimized by the jitter into a no-op, and if it's NULL then the resulting DBNull.Value.ToString() expression produces the empty string you want.

不过,如果你的工作不字符串类型,你可以,如果你想具体格式最终做额外的工作,特别是像一个日期时间。

However, if you're working non-string types, you may end up doing extra work, especially with something like a DateTime if you want specific formatting.

这篇关于从类型“为DBNull”转换为类型“string”无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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