I'm having trouble with my project, it's a project where multiple users within the database are involved.
I don't understand how the SqlDataReader works. Even if used twice in my code,
it doesn't show any errors. Somehow, the SqlDataReader executes only the first If and not the second one. Why didn't the data reader execute the second one? Currently, I'm using Visual basic 2019 and SQL Server 2018.
Here is my code:
Imports System.Data.SqlClient
Public Class Loaning_Login
Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Dim conn As New SqlConnection
If txtUsername.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("Please fill in all fields.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
conn.ConnectionString = "Data Source=NECRONOMICON\SQLEXPRESS01;Initial Catalog=Register;Integrated Security=True;"
Try
Dim sql As String = "SELECT Username, Password FROM [Register].[dbo].[RegisterList] WHERE Username='" & txtUsername.Text & "' AND Password='" & txtPassword.Text & "'"
Dim cmd As New SqlCommand(sql, conn)
cmd.Connection = conn
conn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.Read = True Then
sql = "SELECT Username, Password FROM [Register].[dbo].[RegisterList] WHERE Username ='Joshua' " & "WHERE Password ='Maria' "
Me.Hide()
MessageBox.Show("W E L C O M E !")
Loaner_Status10.Show()
If sql = "SELECT Username, Password FROM [Register].[dbo].[RegisterList] WHERE Username = 'Gabby'" & "WHERE Password = 'Nanamin' " Then
Me.Hide()
MessageBox.Show("W E L C O M E !")
Admin_control_interface.Show()
End If
Else
MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show("Failed to connect to databse. System Error:" & ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
If conn.State <> ConnectionState.Closed Then
conn.Close()
End If
End If
End Sub
Upon inputting the Username (Gabby) and password (Nanamin), the Admin_control_interface form must be shown, but the Loaner_Status10 form keeps showing up