Urgent: DBNull error - 'System.InvalidCastException'

Posted by Supernova122 under VB.NET on 12/10/2013 | Points: 10 | Views : 2498 | Status : [Member] | Replies : 6
Using Visual Studio 2008 and generating .net 2.0 framework with VB.net 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 …”




Responses

Posted by: Bandi on: 12/10/2013 [Member] [MVP] Platinum | Points: 25
Posted by: Bandi on: 12/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
try this ?

If Not IsDbNull(dataview(0)(“name”)) AND dataview(0)(“name”) = my_name then 


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Supernova122, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Check
If Not IsDbNull(dataview(0)(“name”)) AndAlso Convert.Tostring(dataview(0)(“name”))= my_name Then


Supernova122, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Supernova122 on: 12/10/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks for the advice. Actually, I have the problem syntax all over the program and they don't have problem before. If I change it one by one, that will take forever. Could this has something to do with Visual Studio 2008 or microsoft.visualbasic.dll library? I am thinking on what is the source of the problem, what could be the change on the environment as I haven't touched the code. Please help. Thanks a lot.

Supernova122, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,please enable
Option Explicit On and Option Strict ON at the Top of the page in vb.net

it will check syntax and type conversion.

please include this. and check it.

Supernova122, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Supernova122 on: 12/10/2013 [Member] Starter | Points: 25

Up
0
Down

Thanks. I will try that.

Supernova122, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response