如何处理“DBNull”类型的转换 输入“字符串” 无效
我需要一些关于如何处理以下问题的期望建议:-我有一个数据字段misc_text_2,其类型为varchar(25)并允许NULL。 现在,如果我使用以下语法,
<asp:Label ID="lblPrinter" runat="server" Text='<%# iif(eval("misc_text_2") is dbnull.value, "", iif(eval("misc_text_2") like "NA", "None", iif(eval("misc_text_2") like "KP1", "Kitchen Printer 1", iif(eval("misc_text_2") like "KP2", "Kitchen Printer 2", iif(eval("misc_text_2") like "KP3", "Kitchen Printer 3", iif(eval("misc_text_2") like "BP1", "Bar Printer 1", iif(eval("misc_text_2") like "BP2", "Bar Printer 2", iif(eval("misc_text_2") like "BP3", "Bar Printer 3", Eval("misc_text_2")))))))))%>'></asp:Label>
我会继续收到错误异常详细信息:System.InvalidCastException:从类型“DBNull”到类型“String”的转换无效。
我知道我错过了一些东西,但是什么......
提前致谢
I need some expect advice on how to handle the following:- I have a data field misc_text_2 that is of type varchar(25) and allows NULL. Now if I use the following syntax
<asp:Label ID="lblPrinter" runat="server" Text='<%# iif(eval("misc_text_2") is dbnull.value, "", iif(eval("misc_text_2") like "NA", "None", iif(eval("misc_text_2") like "KP1", "Kitchen Printer 1", iif(eval("misc_text_2") like "KP2", "Kitchen Printer 2", iif(eval("misc_text_2") like "KP3", "Kitchen Printer 3", iif(eval("misc_text_2") like "BP1", "Bar Printer 1", iif(eval("misc_text_2") like "BP2", "Bar Printer 2", iif(eval("misc_text_2") like "BP3", "Bar Printer 3", Eval("misc_text_2")))))))))%>'></asp:Label>
I keep on getting an error Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.
I know I'm missing something, but what...
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您必须显式检查
DBNull.Value
并自行进行转换。换句话说,构建一个为您执行转换的方法,同时考虑到 DBNull.Value。
You must explicitly check for
DBNull.Value
and do the conversion yourself.In other words, build a method that will do the conversion for you, taking
DBNull.Value
into account.没有回答你的问题,但是:
您确实应该创建一个代码隐藏方法来执行转换。 这将使代码更容易理解和调试,并且可以重用代码。
Not answering your question, but:
You should really create a code behind method that does the conversion. That will make the code easier to understand and debug, and will make it possible to reuse the code.
每次使用 Eval 时,都必须以某种方式插入惰性求值。 为此,请将
iif(eval("misc_text_2") like ...
的每个实例替换为
iif(IsDbNull(eval("misc_text_2")) OrElse eval("misc_text_2")就像 ...
OrElse 将阻止任何将 DbNull 转换为布尔值的尝试,但是,从更基本的角度来看,bang 是最正确的。 ,可能在 ItemDataBound(或 RowDataBound)事件处理程序中
进一步思考...
OP 也可能在其代码中使用
Convert.ToString()
方法,它将把 DBNull 转换为 String.Empty。Each time you use the Eval, you have to shim-in lazy evaluation, somehow. To do so, replace each instance of:
iif(eval("misc_text_2") like ...
with
iif(IsDbNull(eval("misc_text_2")) OrElse eval("misc_text_2") like ...
The
OrElse
will prevent any attempted conversion of a DbNull to boolean. However, from a more fundamental perspective, bang is most-correct. This should all be done code-behind, probably in the ItemDataBound (or RowDataBound) event handler.Upon further reflection...
The O.P. might also make use of the
Convert.ToString()
method in his code, which will convert DBNulls to String.Empty.由于我们有一个为 MS-Dynamics (Solomon) 设置的旧数据库,因此我们处理 null 的方法是在 ASP 或 VB.NET 代码中将它们转换为 null 字符串。
即
消除任何版本的 VB 的问题。
Since we have a legacy database that was set up for MS-Dynamics (Solomon), our method of handling nulls is to convert them to null strings either in the ASP or VB.NET code.
i.e.
Gets rid of the problem for any version of VB.
如果您使用数据集设计器,消除此错误的最简单方法是更改相关列的属性。
NullValue 异常为“空”而不是“抛出异常”。
希望这会对大家有所帮助。
If you are using data set designer, the easiest way to get rid of this error is to change the properties of respected column.
NullValue exception to "Empty" instead of "Throw exception".
hope this will help you all.
要转换 DBNull 或 IsDBNull,您可以尝试以下方法:
将 DBNull 转换为空字符串
(DBNullObj).ToString
创建 DBNull 转换器函数
公共函数 ConvertDBNullToString(DBNullObj As Object) 作为字符串
如果 IsDBNull(DBNullObj) 那么
返回“”
如果结束
返回DBNullObj
End Function
For convert DBNull or IsDBNull you can try this ways:
Convert DBNull to an empty string
(DBNullObj).ToString
Create a DBNull Converter Function
Public Function ConvertDBNullToString(DBNullObj As Object) as string
if IsDBNull(DBNullObj) then
return ""
end if
return DBNullObj
End Function
正如spiritUMTP所建议的,如果您使用数据集设计器,请将DataColumn.NullValue从“抛出异常”更改为“空”或“无”。 我选择了后者,它解决了问题。 我现在只是检查什么都没有(如果 IsNothing(columnFax) 那么...)
As spiritUMTP suggested, if you are using the Dataset Designer, change the DataColumn.NullValue from "Throw exception" to either "empty" or "Nothing". I chose the latter, and it fixed the problem. I now just check for Nothing (If IsNothing(columnFax) then ...)
您可以在 sql 查询中使用 isNull(misc_text_2, '') 返回空字符串而不是 DBNull。
You could in your sql query use isNull(misc_text_2, '') to return en empty string instead of DBNull.