Link to home
Start Free TrialLog in
Avatar of David Megnin
David MegninFlag for United States of America

asked on

How do I fix InvalidCastException?

In my attached Job/Applicant matching application I'm all of a sudden getting thei "Operator '=' is not defined for type 'DBNull' and type 'Integer' (line 177).
It happens when I select a row in the ApplicantsDataGridView (top one) and select a row in the JobTitlesDataGridView (bottom one) then click the Assign Job Title button (btnUpdatePosition)

    Function getJobTitleIndex(ByVal ID As Integer) As Integer
        For Each row As DataGridViewRow In Me.JobTitlesDataGridView.Rows
            If row.Cells(19).Value = ID Then
                Return row.Index
            End If
        Next
    End Function

    Function getApplicantIndex(ByVal ID As Integer) As Integer
        For Each row As DataGridViewRow In Me.ApplicantsDataGridView.Rows
            If row.Cells(5).Value = ID Then    ' <----- ####  The Error happens here ####
                Return row.Index
            End If
        Next
    End Function

Private Sub btnUpdatePosition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdatePosition.Click
        ' ' -=== Assign Worksite to Youth Button ===-
        ' 'Call the function to get the currently selected row of JobTitlesDataGridView
        'Dim myPreviousValue As Long = Me.JobTitlesDataGridView.SelectedRows.Item(0).Cells(19).Value
        ' Stores the current row number
        ' Check if there's any selected row
        'If Not Me.JobTitlesDataGridView.SelectedRows Is Nothing Then 'CurrentRow Is Nothing Then
        If Me.JobTitlesDataGridView.SelectedRows.Count > 0 Then
            'If Not Me.ApplicantsDataGridView.SelectedRows Is Nothing Then 'CurrentRow Is Nothing Then
            If Me.ApplicantsDataGridView.SelectedRows.Count > 0 Then

                Dim SelectedID As Integer = Me.JobTitlesDataGridView.CurrentRow.Cells(19).Value
                Dim SelectedID2 As Integer = Me.ApplicantsDataGridView.CurrentRow.Cells(5).Value

                Me.Cursor = Cursors.WaitCursor
                Call UpdateAll()
                Call RefreshAll()
                Me.Cursor = Cursors.Default

                ' Set the old position
                Dim DGVindex As Integer = getJobTitleIndex(SelectedID)
                Me.JobTitlesDataGridView.ClearSelection()
                Me.JobTitlesDataGridView.Rows(DGVindex).Selected = True
                Me.JobTitlesDataGridView.FirstDisplayedScrollingRowIndex = DGVindex

                Dim DGVindex2 As Integer = getApplicantIndex(SelectedID2)
                Me.ApplicantsDataGridView.ClearSelection()
                Me.ApplicantsDataGridView.Rows(DGVindex2).Selected = True
                Me.ApplicantsDataGridView.FirstDisplayedScrollingRowIndex = DGVindex2

                Me.JobTitlesDataGridView.ClearSelection()
                Me.ApplicantsDataGridView.ClearSelection()

            Else
                Exit Sub
            End If
        End If
        ''Restore the previously selected row after an update.
        'Dim myRowIndex As Integer = getDGindex(myPreviousValue)
        'Me.JobTitlesDataGridView.Rows(myRowIndex).Selected = True
        'Me.JobTitlesDataGridView.FirstDisplayedScrollingRowIndex = myRowIndex

        ''Maybe put here UpdateTotals() then RefreshAll()  'Note for 20090312
        ''UpdateTotals() is already called in UpdateAll()
    End Sub

I jut don't know enough to know how to fix it.  Assistance appreciated.
'There was no problem with this one:
 
    Function getJobTitleIndex(ByVal ID As Integer) As Integer
        For Each row As DataGridViewRow In Me.JobTitlesDataGridView.Rows
            If row.Cells(19).Value = ID Then
                Return row.Index
            End If
        Next
    End Function
 
'The error happens on this one:
 
    Function getApplicantIndex(ByVal ID As Integer) As Integer
        For Each row As DataGridViewRow In Me.ApplicantsDataGridView.Rows
            If row.Cells(5).Value = ID Then
                Return row.Index
            End If
        Next
    End Function

Open in new window

InvalidCastException.png
InvalidCastException2.png
InvalidCastException-YouthJob.vb.txt
Avatar of srikanthreddyn143
srikanthreddyn143

Check if row.Cells(5).Value, that value is Null. That exception is because you are comparing a null value with integer.
Avatar of David Megnin

ASKER

Okay.  
I also thought it may have had something to do with me moving the JobTitle column in the ApplicantsDataGridView.  I modified the SQL query that populates the DGV and that column ended up in a different place.
No, there are no NULL values in the Job Title field of the table.
I'm looking at the position of that field now, but I'm not real sure what I'm doing.  ;-)
I think I have the keyApplicantID column moved back into it's original position, I'm trying it out again now.
Okay, I think that fixed it.
Now, if I could just get the selected row to stay selected when I update the grids.  The users are complaining that when they select an applicant and select a Job Title and click "Assign Job Title" they loose track of that applicant they just changed because it jumps to the top of the list and they loose the selection.  Plus it crappy dog slow.
ASKER CERTIFIED SOLUTION
Avatar of srikanthreddyn143
srikanthreddyn143

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think in the code above, you are again making ClearSelection().

One way is you know the last updated Rows Ids. So loop through each row in datagridview and set that row selected true.
Thank you.  I'm going to accept "Check for thecorrect column" as the solution to the original question, but could you please give me an example of the "set that row selected true" code above?
I think I've got too much redundant code unselecting and selecting rows.  It's so messy I can't figure it out.  I wrote very little of the code myself; it's mostly code pasted in from examples and solutions to problems I posted here on EE. ;-)
I'm the sole developer in my organizations so I don't have anyone to check my code for sanity.
Thank you very much!

Loop through each row of grid view and check for the condition
If (gridID = ID) then
DataGridView1.CurrentCell = DataGridView1.Rows(counter).Cells(1)

DataGridView1.Rows(counter).Selected = True
End If
Or else first get the row index by checking the ID Condition like
For each row in gridview
if  (grdID = ID) then
irow = row.index
End If
Next

After that

DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.Rows(iRow).Index

DataGridView1.Refresh()

DataGridView1.CurrentCell = DataGridView1.Rows(counter).Cells(1)

DataGridView1.Rows(counter).Selected = True
Thank you.  I'll give that a try.  

I'm always open to any suggestions.  I'm still learning the basics, so every new challange is a learning process for me.