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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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
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
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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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"
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
EdAuthor Commented:
no its still the same.

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

1900-01-01 00:00:00.000
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

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.....
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.