assigning db nulls to strings

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

Any reason to take this precaution ... ?

Dim s As String

s = IIf(.Item("MyField") Is DBNull.Value, "", .Item("MyField"))
 
Using the named columns instead of indexes or .GetOrdinal is yucky. Also,
instead of "" , I'd recommend string.empty so you don't declare unnecessary
strings.

Otherwise, the main reason to do it is to ensure you don't throw a Null
reference exception.

HTH,

Bill
 
John A Grandy said:
Any reason to take this precaution ... ?

Dim s As String

s = IIf(.Item("MyField") Is DBNull.Value, "", .Item("MyField"))


How do you distinguish between DBNull.Value and zero-length strings? If
there's no difference why do you allow Null values in the database?

Apart from that, I'd do it the way you did. Perhaps in a function:

function DBNull2String(byval o as object) as string
if o is dbnull.value then return ""
return o.tostring
end function
 
I have no control over the design & static pre-populating of the database.
I also have only very limited control over the dynamic populating of the
database.
 
John A Grandy said:
I have no control over the design & static pre-populating of the
database. I also have only very limited control over the dynamic
populating of the database.

Ah, I see.

(as your problem seems to be solved, there's nothing else I can say ;-) )
 

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

Back
Top