• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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
0
megnin
Asked:
megnin
  • 7
  • 5
1 Solution
 
srikanthreddyn143Commented:
Check if row.Cells(5).Value, that value is Null. That exception is because you are comparing a null value with integer.
0
 
megninAuthor Commented:
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.
0
 
megninAuthor Commented:
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.  ;-)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
megninAuthor Commented:
I think I have the keyApplicantID column moved back into it's original position, I'm trying it out again now.
0
 
megninAuthor Commented:
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.
0
 
srikanthreddyn143Commented:
Ok.Check for the correct column. Before that make a condition that

If Not row.Cells(5).Value Is DBNull.Value AndAlso Not row.Cells(5).Value Is Nothing Then
 If row.Cells(5).Value = ID Then
                Return row.Index
            End If
End IF

That will prevent exception.
0
 
srikanthreddyn143Commented:
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.
0
 
megninAuthor Commented:
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.
0
 
megninAuthor Commented:
Thank you very much!
0
 
srikanthreddyn143Commented:

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
0
 
srikanthreddyn143Commented:
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
0
 
megninAuthor Commented:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now