Results 1 to 7 of 7

Thread: [RESOLVED] vb.net Issue with dbnull

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Resolved [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.'
    Name:  2022-09-20_17-42-35.jpg
Views: 448
Size:  19.5 KB

    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.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    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:
    1. If DGVrow.Cells("CBB_QA").Value IsNot DBNull.Value Then
    2.     QADGV = DGVrow.Cells("CBB_QA").Value
    3. Else
    4.     QADGV = String.Empty
    5. 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:
    1. 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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: vb.net Issue with dbnull

    Quote Originally Posted by Grand View Post
    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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    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
  •  



Click Here to Expand Forum to Full Width