dbNull cannot be converted to Long

Posted by D-Bar under ASP.NET on 11/21/2013 | Points: 10 | Views : 954 | Status : [Member] | Replies : 6
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>
&lt;/asp:TemplateField&gt;</pre>

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




Responses

Posted by: Bandi on: 11/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Before attempting any mathematical operations on the dataItem check for DBNull and then replace it with zero...


For suppose varMinutes has minutes values...
IsDBNull(varMinutes) ? 0 : varMinutes


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

D-Bar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer
http://www.codeproject.com/Questions/685777/DBNull-cannot-be-converted-to-Long

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

D-Bar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://stackoverflow.com/questions/18383622/best-way-to-handle-convert-failures

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

D-Bar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
try these
IIf(IsDBNull(LongVariable , Nothing, LongVariable)


or

IIf(IsDBNull(LongVariable , 0, LongVariable)



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

D-Bar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: D-Bar on: 11/22/2013 [Member] Starter | Points: 25

Up
0
Down
Thank you to everyone who responded to my post. I probably should have mentioned I am using SQL server. I learned I can use the keyword "COALESCE" to replace null values with whatever I want, in this case zero. Thanks again.
D-Bar

D-Bar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response