Operator '=' is not defined for type 'DBNull' and string "01/01/1900 00:00:00".

I get the error in the title whenever the objComp3("FW_DATEAUDITED") is null in the db.  I try to handle it as of the code below but it does not work and I get the error  '' Operator '=' is not defined for type 'DBNull' and string "01/01/1900 00:00:00"   whenever FW_DATEAUDITED isnull in the db


            If objComp3("FW_DATEAUDITED") IsNot DBNull.Value Then
                FW_DATEAUDITEDTextBox.Text = objComp3("FW_DATEAUDITED").ToString
            End If

            If objComp3("FW_DATEAUDITED") Is DBNull.Value Then
                FW_DATEAUDITEDTextBox.Text = Nothing
            End If

            If objComp3("FW_DATEAUDITED") = "01/01/1900 00:00:00" Then
                FW_DATEAUDITEDTextBox.Text = Nothing
            End If

Open in new window

EdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
indeed, you cannot compare dbnull directly;

use If IsDBNull(objComp3("FW_DATEAUDITED") ) Then
                FW_DATEAUDITEDTextBox.Text = Nothing
            End If
0
AndyAinscowFreelance programmer / ConsultantCommented:
I think you want this structure (air code - may need slight modification):
If objComp3("FW_DATEAUDITED") IsNot DBNull.Value Then
                FW_DATEAUDITEDTextBox.Text = objComp3("FW_DATEAUDITED").ToString
            else  If objComp3("FW_DATEAUDITED") Is DBNull.Value Then
                FW_DATEAUDITEDTextBox.Text = Nothing
            else  If objComp3("FW_DATEAUDITED") = "01/01/1900 00:00:00" Then
                FW_DATEAUDITEDTextBox.Text = Nothing
            End If

because each if statement in your original is processed even if an earlier one was true
0
EdAuthor Commented:
AndyAinscow - That does not throw an error anymore but it puts    01/01/1900 00:00:00   FW_DATEAUDITEDTextBox.Text    

I want the textbox to be llank if the db value is  01/01/1900 00:00:00
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

AndyAinscowFreelance programmer / ConsultantCommented:
Check just what value is in the objComp3, maybe it isn't what you expect

objComp3("FW_DATEAUDITED") = "01/01/1900 00:00:00"
0
AndyAinscowFreelance programmer / ConsultantCommented:
duh, not looked too closely at the order you did things in.  Does this help?

If objComp3("FW_DATEAUDITED") Is DBNull.Value Then
                FW_DATEAUDITEDTextBox.Text = Nothing
else
            If objComp3("FW_DATEAUDITED") = "01/01/1900 00:00:00" Then
                FW_DATEAUDITEDTextBox.Text = Nothing
else
                FW_DATEAUDITEDTextBox.Text = objComp3("FW_DATEAUDITED").ToString
            End If
0
EdAuthor Commented:
no its still the same.

The date is stored in the db like this...

1900-01-01 00:00:00.000
0
AndyAinscowFreelance programmer / ConsultantCommented:
"1900-01-01 00:00:00.000" is not the same as "01/01/1900 00:00:00"

Try this:
If objComp3("FW_DATEAUDITED") Is DBNull.Value Then
                FW_DATEAUDITEDTextBox.Text = Nothing
else
            If objComp3("FW_DATEAUDITED") = "1900-01-01 00:00:00" Then
                FW_DATEAUDITEDTextBox.Text = Nothing
else
                FW_DATEAUDITEDTextBox.Text = objComp3("FW_DATEAUDITED").ToString
            End If
End If
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EdAuthor Commented:
OK, thanks, I'll go and stay in a dark room for a bit i think.....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.