NULLs from SQL--does this make sense?

P

Phil Sandler

All,

I come across a lot of instances where a nullable field in my database
needs to be handled in the application, and I just want to set them to
an empty value. For example, an integer would be returned as 0, a
string would be returned as "".

I know how to do this from the database side (COALESCE), but I need to
handle it on the application side as well, for cases when I don't have
control of every tier.

This is what I've come up with. I'm trying to figure out cases where
it might not work.

-------------------------------------------
'----CALL
'(assume oDR = datareader which is populated)

m_sEmailFrom = ConvertNull(oDR("Email_From"))

'----FUNCTION
Public Function ConvertNull(ByVal piDatafield As Object)
If IsDBNull(piDatafield) Then
Return Nothing
Else
Return piDatafield
End If
End Function
-------------------------------

ASP.NET seems to use "nothing" as a value and an empty value (0 or "")
interchangably.


Thanks for any insight.

Phil
 
P

Phil Sandler

HiltonG said:
For strings specifically you can try return String.Empty

My hope was to have a generic function to handle generic cases (at
least common ones). So far it seems to work for integers, dates and
strings.

It returns nothing if the parameter is dbnull, and nothing seems to
convert to the "empty" version of each datatype I have tried (date,
int, string).

Anyway, the questions remains if anyone sees a problem with doing it
this way, or if there is a better way?

(Since doing this, I have found convert.toint32 convert.tostring, etc.
and am wondering if it does the same thing.)

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top