Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cast from type 'DBNull' to type 'String' is not valid.

Reply
Thread Tools

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

 
 
Elmo Watson
Guest
Posts: n/a
 
      12-24-2003
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?




 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      12-24-2003
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" <> wrote in message
news:...
> 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?
>
>
>
>



 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      12-24-2003
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-> wrote in message
news:...
> 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" <> wrote in message
> news:...
> > 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?
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      12-25-2003
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" <> wrote in message
news:%...
> 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-> wrote in message
> news:...
> > 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" <> wrote in message
> > news:...
> > > 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?
> > >
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cast from type 'DBNull' to type 'String' is not valid. tshad ASP .Net 6 12-15-2004 05:10 PM
Re: Cast from type 'DBNull' to type 'Boolean' is not valid Mike Newton ASP .Net 0 07-27-2004 08:41 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
Cast from type 'DBNull' to type 'String' is not valid error Rob ASP .Net Datagrid Control 0 07-28-2003 09:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57