Need help extracting variables from a While Loop in VB.Net

I’m a novice with my coding so forgive me if my question seems basic but I’m having some trouble extracting my variables from this While Loop in order to then use the results of my SQL query for validation.

This script below is the event handling for a login button on an .aspx form processing an email and login field that will be listed in a correlating MSSQL database:

Public Class _Default
Inherits System.Web.UI.Page

Protected Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
    Dim Column1 As String
    Dim Column2 As String
    Dim SQL = "SELECT * FROM Logins WHERE Email='" & email.Text & "' AND Password='" & password.Text & "'"

    Dim oSqlDataReader As System.Data.SqlClient.SqlDataReader = Nothing
    Using oSqlConnection As New System.Data.SqlClient.SqlConnection("SERVER=[Server Name];UID=[User];PWD=[Pass];DATABASE=[Database Name]")
        oSqlConnection.Open()
        Using oSqlCommand As New System.Data.SqlClient.SqlCommand(SQL, oSqlConnection)
            oSqlDataReader = oSqlCommand.ExecuteReader
            While oSqlDataReader.Read
                Column1 = oSqlDataReader(name:="Email")
                Column2 = oSqlDataReader(name:="Password")
            End While
        End Using
        oSqlConnection.Close()
    End Using

    If "Column 1 etc....."
    End if

End Sub

End Class

As far as I can tell my code is working with no errors but everytime I try and create an If statement my Variable ‘Column 1 and Column 2’ are undeclared making them useless.

If anyone could help with the correct layout for my code or missing areas and an explanation as to where I’ve gone wrong that’d be great.


Source: sql

Leave a Reply