Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hiya peeps!

I have a problem. I have two IF statments but neither are working.

Here is the code I have used.

Code:

Private Sub ViewUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try

        Dim connection As New MySqlConnection("server=" & My.Settings.MysqlHost & ";user id=" & My.Settings.MysqlUsername & "; password=" & My.Settings.MysqlPassword & "; port=3306; database=" & My.Settings.MysqlDatabase & "; pooling=false")
        connection.Open()

        Dim usersql As New MySqlCommand("SELECT * FROM `accounts` WHERE `id` = '" & My.Settings.VUID & "' LIMIT 1", connection)
        usersql.ExecuteNonQuery()

        Dim userreader = usersql.ExecuteReader()

        While (userreader.Read())
            Dim name As String = userreader.GetString(1) & " " & userreader.GetString(2)
            Dim accounttype = ""
            Dim activity = userreader.GetString(8)

            accname.Text = StrConv(name, VbStrConv.ProperCase)
            accid.Text = userreader.GetString(0)
            accemail.Text = userreader.GetString(3)
            accusername.Text = userreader.GetString(4)

            If userreader.GetString(9).Equals(1) Then
                accounttype = "Tenant"
                setpaid.Hide()
                setunpaid.Hide()
            ElseIf userreader.GetString(9).Equals(2) Then
                accounttype = "Landlord"
            End If

            If activity.Equals(1) Then
                accactive.Text = "Suspended"
                suspenduser.Hide()
            Else
                accactive.Text = "Active"
                unsuspend.Hide()
            End If

            acctype.Text = accounttype
            usersql.Dispose()
        End While
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    End Try
End Sub




That is the whole code snippet. Everything is working. But this;

Code:

If userreader.GetString(9).Equals(1) Then
    accounttype = "Tenant"
    setpaid.Hide()
    setunpaid.Hide()
ElseIf userreader.GetString(9).Equals(2) Then
    accounttype = "Landlord"
End If

If activity.Equals(1) Then
    accactive.Text = "Suspended"
    suspenduser.Hide()
Else
    accactive.Text = "Active"
    unsuspend.Hide()
End If




If anyone has any ideas please help!

Many thanks,

James.
Posted
Comments
Henry Minute 26-Sep-10 13:48pm    
The x should be replaced by whatever number you are currently using, 9 in the code you posted.

Well the most obvious thing is that you are reading a string and trying to check if it is equal to an integer, which it never will be.

Either test for .Equals("1") or use GetInt(x).Equals(1).
 
Share this answer
 
Comments
jamesxg1 26-Sep-10 11:01am    
Hiya,

Thank you for a very prompt response, I was just wondering how I would use this? Also what do I replace the 'x' with?

Many many thanks,

James.
you might also want to use a select case, instead of nested if statements. combine this with a ctype to convert an string into an integer.

SQL
Select Case CType(userreader.GetString(9), Integer)

    Case 1

    Case 2

    Case 9

    Case Else

End Select
 
Share this answer
 
Comments
Dalek Dave 29-Sep-10 3:38am    
I would have suggested the same, much easier to control.
Adding to the previous answer, I would put break points on the if statements and step through the code.

This way you can see what is happening or changing line by line.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900