How to deal with DBNull's

  • Thread starter Thread starter Mr Newbie
  • Start date Start date
M

Mr Newbie

Hi,

I have a strongly typed dataset and when I am trying to process this, one
field is empty ( which is correct at this stage ), but I need to test to see
if it has a value before I try and assign it otherwise it throws an
exception, I have tried the following and a few variations but cant work out
how to test this, any ideas ?

If expRow.DateAuthorised = Nothing Then
Me.AuthorisedDate.Text = expRow.DateAuthorised.Date.ToString

End If
 
IsDBNull(field) is how to test for nulls

"If foo is nothing" will not work.
 
Mr Newbie said:
I have a strongly typed dataset and when I am trying to process this, one
field is empty ( which is correct at this stage ), but I need to test to
see if it has a value before I try and assign it otherwise it throws an
exception, I have tried the following and a few variations but cant work
out how to test this, any ideas ?

If expRow.DateAuthorised = Nothing Then
Me.AuthorisedDate.Text = expRow.DateAuthorised.Date.ToString

\\\
If IsDBNull(expRow.DateAuthorized) Then
Me.AutorizedDate.Text = expRow.DateAuthorized.Date.ToString()
End If
///

Alternatively you can use 'If expRow.DateAuthorized Is DBNull.Value
Then...'.
 
Hi,

Mr Newbie said:
Hi,

I have a strongly typed dataset and when I am trying to process this, one
field is empty ( which is correct at this stage ), but I need to test to
see if it has a value before I try and assign it otherwise it throws an
exception, I have tried the following and a few variations but cant work
out how to test this, any ideas ?

If expRow.DateAuthorised = Nothing Then

If you use a Typed DataSet and assuming that DateAuthorised can be null,
then you will also have the following functions :
expRow.IsDataAuthorisedNull()
expRow.SetDateAuthorisedNull()

So, you need to do:

If ( Not expRow.IsDateAuthorisedNull() ) Then
Me.AuthorisedDate.Text = expRow.DateAuthorised.Date.ToString
End If

Accessing the property when it's DBNull.Value results in an Exception,
because the poperty-get tries to cast the DBNull.Value into a DateTime which
can't be done.

HTH,
Greetings
 
Mr Newbie,

I don't know why however I use forever

If Not ExpRow.DataAuthorised Is DbNull.Value then

Just as alternative

Cor
 

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

Similar Threads


Back
Top