Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Cast from type 'DBNull' to type 'String' is not valid. (http://www.velocityreviews.com/forums/t69429-cast-from-type-dbnull-to-type-string-is-not-valid.html)

Elmo Watson 12-24-2003 05:54 PM

Cast from type 'DBNull' to type 'String' is not valid.
 
I've got a 'helper' function created, to modify the showing of database
results:

Function FixLegal(sItem as String, sPrefix as String)
if sItem is System.DBNull.Value or sItem = "" then
FixLegal=""
else
FixLegal="<i>" & sPrefix & ":</i> " & sItem
End If

End Function

Then, in my DataList:

<%# FixLegal(Container.DataItem("PrpSection"), "Section") %>

This works great, unless there's a Null Value in the table for that
field - - when the value is Null - - I get the error in the subject. I
THOUGHT System.DBNull.Value was supposed to take care of this anomaly

Any ideas?





Scott M. 12-24-2003 06:26 PM

Re: Cast from type 'DBNull' to type 'String' is not valid.
 
Try changing your code to read like this:

Function FixLegal(sItem as String, sPrefix as String) As String
if IsDBNull(sItem) or sItem = "" then
Return ""
else
Return "<i>" & sPrefix & ":</i> " & sItem
End If
End Function

**Note that in your original function you never indicated what the function
return data type was.

"Elmo Watson" <sputnik75043@nospamYahoo.com> wrote in message
news:eJ2ptbkyDHA.1616@TK2MSFTNGP11.phx.gbl...
> I've got a 'helper' function created, to modify the showing of database
> results:
>
>
> Then, in my DataList:
>
> <%# FixLegal(Container.DataItem("PrpSection"), "Section") %>
>
> This works great, unless there's a Null Value in the table for that
> field - - when the value is Null - - I get the error in the subject. I
> THOUGHT System.DBNull.Value was supposed to take care of this anomaly
>
> Any ideas?
>
>
>
>




bruce barker 12-24-2003 09:40 PM

Re: Cast from type 'DBNull' to type 'String' is not valid.
 
this will still fail as vb does not short cut if's , try:


Function FixLegal(sItem as String, sPrefix as String)
if sItem is System.DBNull.Value then
FixLegal=""
elseif sItem = "" then
FixLegal=""
else
FixLegal="<i>" & sPrefix & ":</i> " & sItem
End If

End Function




"Scott M." <s-mar@BADSPAMsnet.net> wrote in message
news:uWr7stkyDHA.3436@tk2msftngp13.phx.gbl...
> Try changing your code to read like this:
>
> Function FixLegal(sItem as String, sPrefix as String) As String
> if IsDBNull(sItem) or sItem = "" then
> Return ""
> else
> Return "<i>" & sPrefix & ":</i> " & sItem
> End If
> End Function
>
> **Note that in your original function you never indicated what the

function
> return data type was.
>
> "Elmo Watson" <sputnik75043@nospamYahoo.com> wrote in message
> news:eJ2ptbkyDHA.1616@TK2MSFTNGP11.phx.gbl...
> > I've got a 'helper' function created, to modify the showing of database
> > results:
> >
> >
> > Then, in my DataList:
> >
> > <%# FixLegal(Container.DataItem("PrpSection"), "Section") %>
> >
> > This works great, unless there's a Null Value in the table for that
> > field - - when the value is Null - - I get the error in the subject. I
> > THOUGHT System.DBNull.Value was supposed to take care of this anomaly
> >
> > Any ideas?
> >
> >
> >
> >

>
>




Scott M. 12-25-2003 03:30 AM

Re: Cast from type 'DBNull' to type 'String' is not valid.
 
Can you elaborate? Your code does not include a return type for the
function, has a clause to set sItem to exactly the same value that it has
during a test (so that elseIf is not needed at all) and doesn't use the
"Return" syntax.

But assuming you corrected these things, what is different about your code
and mine?

VB does include the OrElse logical operand for shortcutting but it wouldn't
be needed here.


"bruce barker" <nospam_brubar@safeco.com> wrote in message
news:%23GLqBamyDHA.3216@TK2MSFTNGP11.phx.gbl...
> this will still fail as vb does not short cut if's , try:
>
>
> Function FixLegal(sItem as String, sPrefix as String)
> if sItem is System.DBNull.Value then
> FixLegal=""
> elseif sItem = "" then
> FixLegal=""
> else
> FixLegal="<i>" & sPrefix & ":</i> " & sItem
> End If
>
> End Function
>
>
>
>
> "Scott M." <s-mar@BADSPAMsnet.net> wrote in message
> news:uWr7stkyDHA.3436@tk2msftngp13.phx.gbl...
> > Try changing your code to read like this:
> >
> > Function FixLegal(sItem as String, sPrefix as String) As String
> > if IsDBNull(sItem) or sItem = "" then
> > Return ""
> > else
> > Return "<i>" & sPrefix & ":</i> " & sItem
> > End If
> > End Function
> >
> > **Note that in your original function you never indicated what the

> function
> > return data type was.
> >
> > "Elmo Watson" <sputnik75043@nospamYahoo.com> wrote in message
> > news:eJ2ptbkyDHA.1616@TK2MSFTNGP11.phx.gbl...
> > > I've got a 'helper' function created, to modify the showing of

database
> > > results:
> > >
> > >
> > > Then, in my DataList:
> > >
> > > <%# FixLegal(Container.DataItem("PrpSection"), "Section") %>
> > >
> > > This works great, unless there's a Null Value in the table for that
> > > field - - when the value is Null - - I get the error in the subject. I
> > > THOUGHT System.DBNull.Value was supposed to take care of this anomaly
> > >
> > > Any ideas?
> > >
> > >
> > >
> > >

> >
> >

>
>





All times are GMT. The time now is 06:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.