'DBNull' and type 'Boolean' Error

ggreekggod

Member
Joined
Sep 6, 2007
Messages
7
Programming Experience
1-3
I have a datagrid with a checkbox column. When a user checks a box, leaves the row, re-enters the row and unchecks the box get the following error:

Operator '=' is not defined for type 'DBNull' and type 'Boolean'

The value of the checkbox changes from true or false to DBNull.

How do I avoid this?
 
Hmm.. It's not something that I've seen, but I recommend this:

Run your project in Debug mode (Show the Build toolbar and ensure dropdown set to Debug)
On the Debug menu, choose Exceptions...
Put a tick next to CLR Exceptions in the THROWN column


Reproduce the error
THe code will halt as soon as the exception is raised, regardles sof the datagridview handling it. What code is that, that is throwing the error? What is the Call Stack at the time?
 
Here is the code that causes the exception:

VB.NET:
Private Sub grdSelect_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles grdSelect.CellValueChanged
        If Not mtfLoading Then
            Dim checkCell As DataGridViewCheckBoxCell

            checkCell = CType(grdSelect.Rows(e.RowIndex).Cells("Select"), DataGridViewCheckBoxCell)

          [COLOR="Red"]  If checkCell.Value = True Then[/COLOR]
                Call EnableObjects(True)
                Call Rebind()

            Else
                Call EnableObjects(False)
            End If
        End If
    End Sub

checkCell.Value has a value of DBNull causing the following exception:

System.InvalidCastException occurred
Message="Operator '=' is not defined for type 'DBNull' and type 'Boolean'."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(Object Left, Object Right, Boolean TextCompare)
at pcs.SystemManager.UI.frmReportSetup.grdSelect_CellValueChanged(Object sender, DataGridViewCellEventArgs e) in C:\STWorking\System Manager\Source\Modules\SM\Screens\ReportSetup\pcsWAARptSetup.NET\frmReportSetup.vb:line 2903

One note: It happens when you check and then uncheck whether or not I leave the row. Somehow it is losing its value and being set to null.
 
Last edited by a moderator:
First, the valueChanged event is throw for every cell of the row, so you should use the e.column variable to see if you should process a change or just exit the event handler.

This might be the source of your problem, but otherwise, try using the call stack windows in the debugger to see exactly how the ValueChanged event is reached. It might give you a clue about what is wrong, maybe it executes during an initialisation where the mtfLoading flag is not turned on.
 
cjard,

I agree that it doesn't make alot of sense. I don't know why the checkbox is even needed, should just pick the row and enter info if needed otherwise leave blank (I assume it is just so the user sees that everything is empty and needs to check the box to edit).

Stonkie,

When I was testing I never clicked on a different column so this shouldn't matter, but I'll try it anyway.

Not sure how to get to call stack window in debugger. I'll try to figure it out.
 
Avoid deviating from standard HCI expectations too much; it will only confuse the users. If youre working to a spec, argue that it's not good HCI sense..
 
The call stack window is under Debug -> Windows -> Call Stack when the debugger is started. It will tell you what event handlers/methods lead to your error and hopefully to its source...

You should also make sure that the column in the database's table cannot take null values, this once caused me much trouble because it did load the data correctly but simply didn't change the checkbox's checked state leaving it an undertermined state without throwing an error.
 
Last edited:
I finally came up with a solution. I don't know how good it is but it seems to work.

VB.NET:
If Not mtfLoading Then
            If IsDBNull(grdSelect.Rows(e.RowIndex).Cells("Select").Value) Then
                Call EnableObjects(False)
            Else
                If CType(grdSelect.Rows(e.RowIndex).Cells("Select"), DataGridViewCheckBoxCell).Value = True Then
                    Call EnableObjects(True)
                Else
                    Call EnableObjects(False)
                End If
            End If
        End If

Thanks for your help!!
 
Back
Top