Select Case Statement Not Working When fetching data from sql server

Pardon me asking, I'm stuck where my if else and select case statement will only go to ELSE option. Below is my code

lblStudentName.Text = CStr(studentInfo.stuFullName)
lblStudentBirthCert.Text = CStr(studentInfo.birthCert)

Dim conn As New SqlConnection
Dim cmd As New SqlCommand

conn.ConnectionString = ("server = A-PC\SQLEXPRESS;database=studentManagement;integrated security=true")
conn.Open()

cmd.Connection = conn
cmd.Parameters.AddWithValue("@id", lblStudentBirthCert.Text)
cmd.CommandText = ("SELECT paymentType From payment Where birthcert=@id")

Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim tempPaymentType As String

While reader.Read()
    Dim tempPaymentType = reader.GetString(0)
    Select Case tempPaymentType
        Case "a"
            txtFeeA.Text = tempPaymentType
        Case "b"
            txtFeeB.Text = tempPaymentType
        Case "c"
            txtFeeC.Text = tempPaymentType
        Case "d"
            txtFeeD.Text = tempPaymentType
        Case "e"
            txtFeeE.Text = tempPaymentType
        Case Else
            txtFeeF.Text = tempPaymentType
    End Select
End While
reader.Close()

For some reason, it will only go to else and print it out inside txtFeeF.text. The query will return 6 result btw, a to f. It is the same with if else statement. Without any if else and select statement, the the output will be f, which is the last in the reader. Any help is greatly appreciated.

If tempPaymentType.Equals("a") Then
            txtFeeA.Text = tempPaymentType.ToString
        End If

Answers


Try Adding

Trim().ToLower() 

after your

GetString(0) 

to see if there are case issue or extra spaces. If the database field is a character field, it will add extra spaces, but if VARCHAR(), it should trim it..


Add a line to help you debug:

    Dim tempPaymentType = reader.GetString(0)
    Debug.Print (String.Format("tempPaymentType: {0}", tempPaymentType))
    Select Case tempPaymentType

There could be a lot of reasons, but you haven't given enough information. For example are the letters different cases? ("a" vs. "A")

It may help to change this code:

Dim tempPaymentType As String

While reader.Read()
    tempPaymentType = reader.GetString(0)

I'm surprised it even builds with the extra Dim statement.


Need Your Help

Single Sign On using NTLMv1 or NTLMv2 or kerberos to a Spring webapp on tomcat on linux

tomcat single-sign-on kerberos ntlm ntlmv2

We make an appliance with a spring web interface running on Tomcat on Centos Linux.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.