[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

10.0

How do I fix InvalidCastException?

Asked by megnin in VB Controls, Microsoft Visual Basic.Net

Tags: VB 2008, DataGridView

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.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
'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
Attachments:
 
InvalidCastException was unhandled
InvalidCastException was unhandled
 
 
Application Form
Application Form
 
 
 
[+][-]04/20/09 03:40 PM, ID: 24189428Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: VB Controls, Microsoft Visual Basic.Net
Tags: VB 2008, DataGridView
Sign Up Now!
Solution Provided By: srikanthreddyn143
Participating Experts: 1
Solution Grade: A
 
[+][-]04/20/09 03:02 PM, ID: 24189176Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/20/09 03:18 PM, ID: 24189292Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/20/09 03:26 PM, ID: 24189341Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/20/09 03:33 PM, ID: 24189386Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/20/09 03:39 PM, ID: 24189419Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/20/09 03:45 PM, ID: 24189454Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/21/09 05:14 AM, ID: 24193377Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/21/09 06:42 AM, ID: 24194181Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/21/09 06:45 AM, ID: 24194230Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/21/09 07:07 AM, ID: 24194525Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625