-
Sep 20th, 2022, 10:48 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] vb.net Issue with dbnull
Hi
I am sure it is my fault but I don't know why is this happening and how to fix it.
So, I am checking some cells in a datagridview and the ones that are changed from some text to nothing are giving me errors:
Code:
If Not IsDBNull(DGVrow.Cells("CBB_QA")) Then
QADGV = DGVrow.Cells("CBB_QA").Value
Else
QADGV = ""
End If
System.InvalidCastException: 'Conversion from type 'DBNull' to type 'String' is not valid.'
I think I am specifically checking that if it is not DBnull then go to the next line and read the value. The code verifies that the cell is not dbnull but then in the next line it tells me that conversion from dbnull to string is not valid.
What is happening and how can I fix it.
The cell is a combobox column in a datagridview and in these cases users changed the previous values to nothing.
Thank for help
Last edited by dday9; Sep 20th, 2022 at 01:46 PM.
-
Sep 20th, 2022, 12:54 PM
#2
Re: vb.net Issue with dbnull
I think the pattern is something like this,
Code:
If DBNull.Value.Equals(valueToCheck) Then
Stop
Else
Stop
End If
-
Sep 20th, 2022, 12:59 PM
#3
Re: vb.net Issue with dbnull
The problem is that you're checking the cell... which is an object and seeing if it is null or not. What you should be checking is if the VALUE is null.
-tg
-
Sep 20th, 2022, 08:40 PM
#4
Re: vb.net Issue with dbnull
As suggested, the main issue is that you're comparing the cell itself, rather than the Value of the cell. Even with that fix, you can still improve the structure of the code. Don't use the IsDBNull method. Compare directly:
vb.net Code:
If DGVrow.Cells("CBB_QA").Value IsNot DBNull.Value Then
QADGV = DGVrow.Cells("CBB_QA").Value
Else
QADGV = String.Empty
End If
The thing is, DBNull.ToString returns String.Empty anyway, so if you want a String value from a cell or an empty String if it's NULL, just call ToString:
vb.net Code:
QADGV = DGVrow.Cells("CBB_QA").Value.ToString()
That will have the same effect.
Also, I suspect that QADGV is type String, which means that you must have Option Strict Off for that code to compile. You should turn Option Strict On immediately, in both the project properties and the VS options, so it will be On by default for all future projects. That will forc eyou to write better code by paying more attention to your data types, preventing certain errors being missed and potentially causing crashes or corrupt data at run time.
-
Sep 21st, 2022, 01:54 AM
#5
Thread Starter
Fanatic Member
Re: vb.net Issue with dbnull
Again
Than you so much all. It works and I understood the issue.
I also was not aware of "Option Strict" thing, I will do that to.
-
Sep 21st, 2022, 01:58 AM
#6
Re: vb.net Issue with dbnull
Originally Posted by Grand
I also was not aware of "Option Strict" thing, I will do that to.
Unfortunately, Option Strict Off is the default and most tutorials and many courses don't mention it until fairly late on or even not at all. That default does make it easier for beginners to get writing code quickly and it is also in line with how VB6 worked, but it also allows you to write bad code more easily. The sooner you turn it On, the sooner you will be forced to put the appropriate amount of thought into the data types you use.
-
Sep 21st, 2022, 02:31 AM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] vb.net Issue with dbnull
Thanks for the explanation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|