lundi 21 décembre 2015

VB.Net Code exits routine on false if statement without executing 'Else' command

I'm using the following code to connect to an Access database and check if user's names are added to a table, if they are, then depending on which table they belong to directly affects their access levels to the software.

Public Sub Check_Database_For_Access_Level(Name As String)


    Dim myCon = New OleDbConnection(My.Settings.Database_Connection_String)
    myCon.Open()
    Dim dr As OleDbDataReader


    Dim Str

    Str = "SELECT * FROM [Admin] WHERE [Emp Name]=?"
    Dim cmd As OleDbCommand = New OleDbCommand(Str, myCon)
    cmd.Parameters.AddWithValue("Emp Name", Name)
    dr = cmd.ExecuteReader
    dr.Read()

    If dr("Emp Name").ToString = Name Then 'ERROR HERE
        My.Settings.Setting_AccessLevel = "Administrator"
    Else
        Str = "SELECT * FROM [ReadWrite] WHERE [Emp Name]=?"
        cmd.Parameters.AddWithValue("Emp Name", Name)
        dr = cmd.ExecuteReader
        dr.Read()

        If dr("Emp Name").ToString = Name Then
            My.Settings.Setting_AccessLevel = "Read And Write"
        Else

        End If

    End If



    myCon.Close()
End Sub

The line:

If dr("Emp Name").ToString = Name Then 'ERROR HERE

operates as it is supposed to if the result is true, but if it is false, the sub simply exits?

How come it isn't firing the Else part? Even stepping through it simply exits.

Aucun commentaire:

Enregistrer un commentaire