Using Visual Studio 2008 and generating .net 2.0 framework with for a web application.

I encountered a very strange problem currently. After I build a solution and click a link in a web page, I got an error message as following. The same thing happened when I tried to run in debug mode.

************* Error ***********************************************************************************************
"An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code"

"Additional information: Operator '=' is not defined for type 'DBNull' and string "". "

The problem part is simply as below in a vb file. The value of dataview(0)(“name”) is NULL and my_name is a variable string. The strange is thing I didn’t change any code in this vb file. This vb file was built previously and used for a long time ago without any problem. It just suddenly happened today. I have this syntax all over the program without causing any problem. I have tried to restore the whole solution back to original but still have the problem.
Can anyone please advise why all the sudden and how to fix? Thanks.

“If dataview(0)(“name”) = my_name then …”

Recommended Answers

All 2 Replies

The error seems straightforward to me. String and DBNull are incompatible types, so you need to handle them separately:

If dataview(0)("name") = DBNull.Value Then
    ' Handle a null name
    ' Handle a non-null name
End If

How you handle the name depends on what your "..." part does.

Thanks for your advice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.