Case statement fails case when it is not supposed to

I have setup a case statement and the statement is bypassed when it is not supposed to be bypassed.  I have checked the data I am compairing for and it does meet the criteria.  When I debug and step through the code, it hits Case grade=9 and then steps to the case grade = 10.  The actual data is grade="9" when I look at the local variables.  I have tried useing the val command to convert it to a value but this did not help.  Can someone point out what I may be missing.  Here is the code.

Thanks
Kenny


'Calculate Objective Scores Totals

            Select Case grade

Case grade = "9"

                    readingEla9AvailableObj1 = 9
                    readingEla9AvailableObj2 = 15
                    readingEla9AvailableObj3 = 18
                    readingEla9TotalAvailable = 42

                    math9AvailableObj1 = 5
                    math9AvailableObj2 = 5
                    math9AvailableObj3 = 5
                    math9AvailableObj4 = 5
                    math9AvailableObj5 = 4
                    math9AvailableObj6 = 4
                    math9AvailableObj7 = 4
                    math9AvailableObj8 = 6
                    math9AvailableObj9 = 5
                    math9AvailableObj10 = 9
                    math9TotalAvailable = 52

                    readingElaObj91ObjectiveGrade = CalculatePF(readingEla9AvailableObj1, readingElaObjScoresArray(1))
                    readingElaObj92ObjectiveGrade = CalculatePF(readingEla9AvailableObj2, readingElaObjScoresArray(2))
                    readingElaObj93ObjectiveGrade = CalculatePF(readingEla9AvailableObj3, readingElaObjScoresArray(3))

                    MathObj91ObjectiveGrade = CalculatePF(math9AvailableObj1, mathObjScoresArray(1))
                    MathObj92ObjectiveGrade = CalculatePF(math9AvailableObj2, mathObjScoresArray(2))
                    MathObj93ObjectiveGrade = CalculatePF(math9AvailableObj3, mathObjScoresArray(3))
                    MathObj94ObjectiveGrade = CalculatePF(math9AvailableObj4, mathObjScoresArray(4))
                    MathObj95ObjectiveGrade = CalculatePF(math9AvailableObj5, mathObjScoresArray(5))
                    MathObj96ObjectiveGrade = CalculatePF(math9AvailableObj6, mathObjScoresArray(6))
                    MathObj97ObjectiveGrade = CalculatePF(math9AvailableObj1, mathObjScoresArray(7))
                    MathObj98ObjectiveGrade = CalculatePF(math9AvailableObj2, mathObjScoresArray(8))
                    MathObj99ObjectiveGrade = CalculatePF(math9AvailableObj3, mathObjScoresArray(9))
                    MathObj910ObjectiveGrade = CalculatePF(math9AvailableObj4, mathObjScoresArray(10))

                    Try
                        Dim objConnection6 As SqlConnection = New SqlConnection("server=(local);database=FBISD-TAKS;user id=sa;password=pa55word")
                        Dim objCommand6 As SqlCommand = New SqlCommand
                        objCommand6.Connection = objConnection6
                        objCommand6.CommandText = "Insert Into objectivesCorrect " & "(admDate) " & _
                                "VALUES(@oadmDate)"
                        objCommand6.Parameters.Add("@oadmDate", SqlDbType.DateTime, 10).Value = madmDate
                        objCommand6.Parameters.Add("@ostudentId", SqlDbType.Int, 10).Value = studentId
                        objCommand6.Parameters.Add("@oteacherId", SqlDbType.Int, 8).Value = teacher1
                        objConnection6.Open()
                        Dim kenny As String
                        kenny = objCommand6.CommandText
                        MsgBox(kenny)
                        objCommand6.ExecuteNonQuery().ToString()
                        objConnection6.Close()

                    Catch ex As Exception
                        MsgBox(ex.ToString())

                    End Try




                Case grade = 10
                    readingEla10AvailableObj1 = 8
                    readingEla10AvailableObj2 = 11
                    readingEla10AvailableObj3 = 18
                    readingEla10AvailableObj6 = 20
                    readingEla10TotalAvailable = 57

                    math10AvailableObj1 = 5

                    ..................etc



               End Select

kwh3856Asked:
Who is Participating?
 
wguerramConnect With a Mentor Commented:
Did you try the trim?

if so and it didn't work try this:

initialize the variable to 9 just before the select case and try it, just to see if you are having other problem.

like:

grade = "9"

 Select Case grade
     Case grade = "9"
         .
         .
     Case grade = "10"
0
 
wguerramCommented:
Try placing a msgbox just before the Select case

Msgbox (grade)

if the variable is numeric

Msgbox (grade.tostring)

just to see what is the value.

How do you obtain the value for the grade variable?
can you post the code?
0
 
kwh3856Author Commented:
I will try that and let you know.  Here is the code where I get the grade.

objStreamReader = New StreamReader("C:\fbisd\t178914a.dat")
        strLine = objStreamReader.ReadLine
        Do While Not strLine Is Nothing
            admDate = Mid(strLine, 1, 4)
            admDate = admDate.Substring(0, 2) & "/01/20" & admDate.Substring(2)
            madmDate = admDate
            grade = Val(Mid(strLine, 5, 2))


0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
wguerramCommented:
What type of value is the variable grade?
0
 
kwh3856Author Commented:
The message box showed a value of 9.  It was not in quotes. Yet when you look at the local varibles for grade...it show it in quotes
0
 
kwh3856Author Commented:
I read the grade in as a string variable and then covert it to a numeric value.  It is actually the grade level a student is currently in.
0
 
wguerramCommented:
Are you declaring grade variable or not before using it?

like:

dim grade as string

or
 dim grade as integer

?
0
 
kwh3856Author Commented:
I dim the grade as string originally
0
 
kwh3856Author Commented:
here is the code

 Dim objStreamReader As StreamReader
        Dim strLine As String
        Dim myConnString As String
        Dim admDate As String
        Dim madmDate As DateTime
        Dim grade As String
0
 
wguerramCommented:
Try adding a trim, in the select case

 Select Case grade.trim
     Case grade = "9"
         .
         .
     Case grade = "10"
0
 
wguerramCommented:
You don't need to convert the value to numeric

grade = Val(Mid(strLine, 5, 2))

just leave it like

grade = Mid(strLine, 5, 2)
0
 
kwh3856Author Commented:
That did not work either.  I tried the opposite with no success.  Here is how my code currently sits.

Sub Main()
        Dim objStreamReader As StreamReader
        Dim strLine As String
        Dim myConnString As String
        Dim admDate As String
        Dim madmDate As DateTime
        Dim grade As String
        Dim cvtgrade As Int16


objStreamReader = New StreamReader("C:\fbisd\t178914a.dat")
        strLine = objStreamReader.ReadLine
        Do While Not strLine Is Nothing
            admDate = Mid(strLine, 1, 4)
            admDate = admDate.Substring(0, 2) & "/01/20" & admDate.Substring(2)
            madmDate = admDate
            grade = Mid(strLine, 5, 2)
            cvtgrade = Val(grade)


            cvtgrade = 9

            'Calculate Objective Scores Totals
            MsgBox(cvtgrade)

            Select Case cvtgrade

                Case cvtgrade = 3
                    readingEla3AvailableObj1 = 15
                    readingEla3AvailableObj2 = 7
                    readingEla3AvailableObj3 = 6
                    readingEla3AvailableObj4 = 8
                    readingEla3TotalAvailable = 36
                   .......................etc

                Case cvtgrade = 9 --------------------------------------------Fails test and skips to Case cvtgrade = 10
                    readingEla9AvailableObj1 = 9
                    readingEla9AvailableObj2 = 15
                    readingEla9AvailableObj3 = 18
                    readingEla9TotalAvailable = 42


         EndSelect
0
 
kwh3856Author Commented:
I got it!!!!!!!!!!

Here was the problem.............do not include the variable...just tell it the value you are looking for

Case 9

                    readingEla9AvailableObj1 = 9
                    readingEla9AvailableObj2 = 15
                    readingEla9AvailableObj3 = 18
                    readingEla9TotalAvailable = 42
0
 
kwh3856Author Commented:
wguerram

Thanks for your help.  You definately got me going in the right direction..

Thanks
Kenny
0
 
wguerramCommented:
Oh, yes you are right.

i didn't see the difference

look

 Dim x As String

        x = Val("9 ")

        Select Case x.Trim
            Case "9"
                MsgBox("9")
            Case 10
                MsgBox("10")
        End Select

i was making test and wasn't using the variable in the case.

It's great to know you solve your problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.