David Megnin
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.R ows
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(By Val 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.S electedRow s.Item(0). Cells(19). Value
' Stores the current row number
' Check if there's any selected row
'If Not Me.JobTitlesDataGridView.S electedRow s Is Nothing Then 'CurrentRow Is Nothing Then
If Me.JobTitlesDataGridView.S electedRow s.Count > 0 Then
'If Not Me.ApplicantsDataGridView. SelectedRo ws Is Nothing Then 'CurrentRow Is Nothing Then
If Me.ApplicantsDataGridView. SelectedRo ws.Count > 0 Then
Dim SelectedID As Integer = Me.JobTitlesDataGridView.C urrentRow. 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(SelectedI D)
Me.JobTitlesDataGridView.C learSelect ion()
Me.JobTitlesDataGridView.R ows(DGVind ex).Select ed = True
Me.JobTitlesDataGridView.F irstDispla yedScrolli ngRowIndex = DGVindex
Dim DGVindex2 As Integer = getApplicantIndex(Selected ID2)
Me.ApplicantsDataGridView. ClearSelec tion()
Me.ApplicantsDataGridView. Rows(DGVin dex2).Sele cted = True
Me.ApplicantsDataGridView. FirstDispl ayedScroll ingRowInde x = DGVindex2
Me.JobTitlesDataGridView.C learSelect ion()
Me.ApplicantsDataGridView. ClearSelec tion()
Else
Exit Sub
End If
End If
''Restore the previously selected row after an update.
'Dim myRowIndex As Integer = getDGindex(myPreviousValue )
'Me.JobTitlesDataGridView. Rows(myRow Index).Sel ected = True
'Me.JobTitlesDataGridView. FirstDispl ayedScroll ingRowInde x = 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.
InvalidCastException2.png
InvalidCastException-YouthJob.vb.txt
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.R
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.
If row.Cells(5).Value = ID Then ' <----- #### The Error happens here ####
Return row.Index
End If
Next
End Function
Private Sub btnUpdatePosition_Click(By
' ' -=== Assign Worksite to Youth Button ===-
' 'Call the function to get the currently selected row of JobTitlesDataGridView
'Dim myPreviousValue As Long = Me.JobTitlesDataGridView.S
' Stores the current row number
' Check if there's any selected row
'If Not Me.JobTitlesDataGridView.S
If Me.JobTitlesDataGridView.S
'If Not Me.ApplicantsDataGridView.
If Me.ApplicantsDataGridView.
Dim SelectedID As Integer = Me.JobTitlesDataGridView.C
Dim SelectedID2 As Integer = Me.ApplicantsDataGridView.
Me.Cursor = Cursors.WaitCursor
Call UpdateAll()
Call RefreshAll()
Me.Cursor = Cursors.Default
' Set the old position
Dim DGVindex As Integer = getJobTitleIndex(SelectedI
Me.JobTitlesDataGridView.C
Me.JobTitlesDataGridView.R
Me.JobTitlesDataGridView.F
Dim DGVindex2 As Integer = getApplicantIndex(Selected
Me.ApplicantsDataGridView.
Me.ApplicantsDataGridView.
Me.ApplicantsDataGridView.
Me.JobTitlesDataGridView.C
Me.ApplicantsDataGridView.
Else
Exit Sub
End If
End If
''Restore the previously selected row after an update.
'Dim myRowIndex As Integer = getDGindex(myPreviousValue
'Me.JobTitlesDataGridView.
'Me.JobTitlesDataGridView.
''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
InvalidCastException.pngInvalidCastException2.png
InvalidCastException-YouthJob.vb.txt
Check if row.Cells(5).Value, that value is Null. That exception is because you are comparing a null value with integer.
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.
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.
ASKER
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'm looking at the position of that field now, but I'm not real sure what I'm doing. ;-)
ASKER
I think I have the keyApplicantID column moved back into it's original position, I'm trying it out again now.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
One way is you know the last updated Rows Ids. So loop through each row in datagridview and set that row selected true.
ASKER
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.
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.
ASKER
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
DataGridView1.Rows(counter
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.FirstDisplay edScrollin gRowIndex = DataGridView1.Rows(iRow).I ndex
DataGridView1.Refresh()
DataGridView1.CurrentCell = DataGridView1.Rows(counter ).Cells(1)
DataGridView1.Rows(counter ).Selected = True
For each row in gridview
if (grdID = ID) then
irow = row.index
End If
Next
After that
DataGridView1.FirstDisplay
DataGridView1.Refresh()
DataGridView1.CurrentCell = DataGridView1.Rows(counter
DataGridView1.Rows(counter
ASKER
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.
I'm always open to any suggestions. I'm still learning the basics, so every new challange is a learning process for me.