DBNull无法转换为Long [英] DBNull cannot be converted to Long

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

问题描述

大家好,感谢您阅读我的帖子。我使用以下内容将时间分为小时和分钟。



 <   ItemTemplate  >  
< % #Format((CLng(DataBinder.Eval(Container.DataItem, hours )) - (CLng(DataBinder.Eval(Container.DataItem, hours))Mod 60 ))/ 60 0
%>

&升t;% #Format(CLng(DataBinder.Eval(Container.DataItem, hours))Mod 60 00%>
< / ItemTemplate >
< / asp:TemplateField >





工作得很好除非其中一个值为Null,否则抛出错误DBNull无法转换为Long。如果小时为空,我该如何设置条件,什么都不做?



提前付款

D-Bar

解决方案

如果您使用MS SQL服务器数据库作为数据源,则可以使用COALESCE [ ^ ]用默认值替换 NULL 的函数(在本例中为零)。如果Field2 NULL COALESCE 函数返回Field2的值。 />


  SELECT  Field1, COALESCE (Field2, 0  AS  Field2 
FROM TableName


 如果 Convert.IsDBNull(Container.DataItem)然后 
' 其空
结束 如果


您可以更改查询,以便不返回NULL,或者您需要检查类型是否为DBNull(也可以使用 IsDBNull ) 。从数据库返回的值实际上不是空值,而是特定类型DBNull。



http://msdn.microsoft.com/en-us/library/tckcces5%28v=vs.90%29.aspx [ ^ ]



好运气!

Hello all, thanks for reading my post. I am using the following to get minutes into hours and minutes.

<ItemTemplate>
                                                        <%#Format((CLng(DataBinder.Eval(Container.DataItem, "hours")) - (CLng(DataBinder.Eval(Container.DataItem, "hours")) Mod 60)) / 60, "0")
                                                        %>
                                                        :
                                                        <%# Format(CLng(DataBinder.Eval(Container.DataItem, "hours")) Mod 60, "00")%>
                                                        </ItemTemplate>
                                                        </asp:TemplateField>



Works great unless one of the values is Null, then it throws an error DBNull cannot be converted to Long. How can I put a condition in so if hours is null, don't do anything?

Thanks in advance
D-Bar

解决方案

If you use MS SQL server database as a source of data, you can use COALESCE[^] function to replace NULL with default value (in this case with zero). If the Field2 is not NULL, COALESCE function returns the value of Field2.

SELECT Field1, COALESCE(Field2,0) AS Field2
FROM TableName


If Convert.IsDBNull(Container.DataItem) Then
  'its null
End If


You could alter your query so NULL isn't returned or you need to check if the type is DBNull (also possible using IsDBNull). The value returned from the database isn't actually a null value but of a specific type DBNull.

http://msdn.microsoft.com/en-us/library/tckcces5%28v=vs.90%29.aspx[^]

Good luck!


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

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