Issues with calculation/data type

Joined
Mar 19, 2008
Messages
5
Location
Western NY
Programming Experience
1-3
I'm currently developing a VB.net application that keeps track of a users golf scores and calculates round average/current handicap. Everything is working great except I whenever I reach 5 scores, which is the number needed to calculate the handicap I get the following error message:

Conversion from string "DataGridViewTextBoxCell { Column" to type 'Integer' is not valid.

The weird part is the code is the same for all 5 scores but it works for the 1st four and only errors on the 5th one. I verified the code used for all of them is the same except for a change in variable names. I have attached a copy of the code section and would appreciate any help that you guys could provide me. Also the data is being stored in a database and shown on a form using a datagridview control.

VB.NET:
Private Sub CalcHandicap()

        ' determines the last 5 scores, course ratings and course slopes
        ' then calculates the differential for each course and than determines which of the five is the lowest. the lowest of the differentials
        ' is than multiplied by 0.96 to get the current handicap and once determined it will be multiplied by 0.96is added to the handicap
        ' textbox for viewing
        Dim differential1 As Integer = 0
        Dim differential2 As Integer = 0
        Dim differential3 As Integer = 0
        Dim differential4 As Integer = 0
        Dim differential5 As Integer = 0
        Dim round1 As Integer
        Dim round2 As Integer
        Dim round3 As Integer
        Dim round4 As Integer
        Dim round5 As Integer
        Dim currentRowTotal As Integer = TblGolfScoresDataGridView.RowCount - 1
        Dim tempScoreCell As Integer
        Dim tempCourseRatingCell As Integer
        Dim tempCourseSlopeCell As Integer
        Dim score1 As Integer
        Dim score2 As Integer
        Dim score3 As Integer
        Dim score4 As Integer
        Dim score5 As Integer
        Dim rating1 As Integer
        Dim rating2 As Integer
        Dim rating3 As Integer
        Dim rating4 As Integer
        Dim rating5 As Integer
        Dim slope1 As Integer
        Dim slope2 As Integer
        Dim slope3 As Integer
        Dim slope4 As Integer
        Dim slope5 As Integer

        If currentRowTotal >= 4 Then
            round5 = currentRowTotal
            ' grabs the last scores, course ratings and slopes
            tempScoreCell = CInt(TblGolfScoresDataGridView.Rows(round5).Cells(3).ToString)
            tempCourseRatingCell = CInt(TblGolfScoresDataGridView.Rows(round5).Cells(4).ToString)
            tempCourseSlopeCell = CInt(TblGolfScoresDataGridView.Rows(round5).Cells(5).ToString)
            score5 = tempScoreCell
            rating5 = tempCourseRatingCell
            slope5 = tempCourseSlopeCell
            differential5 = CInt(((score5 - rating5) * 113) / slope5)
        End If


        ' decrease rowtotal by 1
        currentRowTotal -= 1

        If currentRowTotal >= 3 Then
            round4 = currentRowTotal
            ' grabs the next score, course rating and slope
            tempScoreCell = CInt(TblGolfScoresDataGridView.Rows(round4).Cells(3).ToString)
            tempCourseRatingCell = CInt(TblGolfScoresDataGridView.Rows(round4).Cells(4).ToString)
            tempCourseSlopeCell = CInt(TblGolfScoresDataGridView.Rows(round4).Cells(5).ToString)
            score4 = tempScoreCell
            rating4 = tempCourseRatingCell
            slope4 = tempCourseSlopeCell
            differential4 = CInt(((score4 - rating4) * 113) / slope4)
        End If

        ' decrease rowtotal by 1
        currentRowTotal -= 1

        If currentRowTotal >= 2 Then
            round3 = currentRowTotal
            ' grabs the next score, course rating and slope
            tempScoreCell = CInt(TblGolfScoresDataGridView.Rows(round3).Cells(3).ToString)
            tempCourseRatingCell = CInt(TblGolfScoresDataGridView.Rows(round3).Cells(4).ToString)
            tempCourseSlopeCell = CInt(TblGolfScoresDataGridView.Rows(round3).Cells(5).ToString)
            score3 = tempScoreCell
            rating3 = tempCourseRatingCell
            slope3 = tempCourseSlopeCell
            differential3 = CInt(((score3 - rating3) * 113) / slope3)
        End If

        ' decrease rowtotal by 1
        currentRowTotal -= 1

        If currentRowTotal >= 1 Then
            round2 = currentRowTotal
            ' grabs the next score, course rating and slope
            tempScoreCell = CInt(TblGolfScoresDataGridView.Rows(round2).Cells(3).ToString)
            tempCourseRatingCell = CInt(TblGolfScoresDataGridView.Rows(round2).Cells(4).ToString)
            tempCourseSlopeCell = CInt(TblGolfScoresDataGridView.Rows(round2).Cells(5).ToString)
            score2 = tempScoreCell
            rating2 = tempCourseRatingCell
            slope2 = tempCourseSlopeCell
            differential2 = CInt(((score4 - rating4) * 113) / slope4)
        End If

        ' decrease rowtotal by 1
        currentRowTotal -= 1

        If currentRowTotal >= 0 Then
            round1 = currentRowTotal
            ' grabs the next score, course rating and slope
            tempScoreCell = CInt(TblGolfScoresDataGridView.Rows(round1).Cells(3).ToString)
            tempCourseRatingCell = CInt(TblGolfScoresDataGridView.Rows(round1).Cells(4).ToString)
            tempCourseSlopeCell = CInt(TblGolfScoresDataGridView.Rows(round1).Cells(5).ToString)
            score1 = tempScoreCell
            rating1 = tempCourseRatingCell
            slope1 = tempCourseSlopeCell
            differential1 = CInt(((score5 - rating5) * 113) / slope5)
        End If

        Dim lowestDiff As Integer

        If differential1 < differential2 Then
            lowestDiff = differential1
        Else
            lowestDiff = differential2
        End If

        If differential3 < lowestDiff Then
            lowestDiff = differential3
        End If

        If differential4 < lowestDiff Then
            lowestDiff = differential4
        End If

        If differential5 < lowestDiff Then
            lowestDiff = differential5
        End If

        txtHandicap.Text = CStr(lowestDiff * 0.96)

    End Sub

Thanks in advance :cool:
 
change this and similar:
TblGolfScoresDataGridView.Rows(round5).Cells(3).ToString
to:
VB.NET:
Me.TblGolfScoresDataGridView.Item(round5, 3).Value
 
Johnh I implemented your suggestion and it works great. However I'm now getting a new error. The error is for an overflow exception. The error involves an arithmetic operation that caused the overflow. Below is the code I'm using which is used 5 times based on 5 different sets of data. I've done the math myself and everything comes out positive. The variables are all created as integer data types.

VB.NET:
differential5 = ((score5 - rating5) * 113) / slope5
 
Overflow mean the result is too big/small to fit the data type. Debug by hovering the variables in the equation when the exception occurs. Review the values and your algorithm. Consider changing the result variable data type if you expect value to exceed +/- 2 billions.
 
Back
Top