this is the code of the from which i trying to open form the login form it is say invalid attempt to call read when reader is closed
Imports System.Data.SqlClient
Public Class Form40
Private Sub Form40_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = users
Dim user_Name As String
Dim main_menu1, main_menu2 As String
If (con.State = ConnectionState.Closed) Then
con.Open()
Dim cmd As New SqlCommand(" SELECT User_name, main_menu1,main_menu2 FROM users WHERE (User_name = '" & TextBox1.Text & "' )", con)
Dim ddr As SqlDataReader = cmd.ExecuteReader()
If ddr.HasRows Then
While ddr.Read()
main_menu1 = ddr("main_menu1").ToString()
main_menu2 = ddr("main_menu2").ToString()
user_Name = ddr("User_Name").ToString()
If user_Name = TextBox1.Text And main_menu1 = "True" Then
TextBox2.Visible = False
End If
End While
End If
ddr.Close()
End If
con.Close()
End Sub
End Class
and this is the code of the form login
Imports System.Data
Imports System.Data.SqlClient
Public Class LoginForm2
Dim cmd As New SqlCommand
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
ConnectToSQL()
End Sub
Private Sub ConnectToSQL()
Dim Pass_word As String
Dim Pass_word2 As String
Dim user_Name As String
Try
con.Open()
Dim cmd As New SqlCommand(" SELECT User_name, Pass_word FROM users WHERE (User_name = '" & UsernameTextBox.Text & "' ) AND (Pass_word = '" & PasswordTextBox.Text & "')", con)
Dim lrd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If lrd.HasRows Then
While lrd.Read()
Pass_word = lrd("Pass_word").ToString()
user_Name = lrd("User_Name").ToString()
Pass_word2 = PasswordTextBox.Text()
If Pass_word = Pass_word2 And user_Name = UsernameTextBox.Text Then
MessageBox.Show("Logged in successfully as " & user_Name, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
users = user_Name
Me.Hide()
Form40.Show()
'Clear all fields
PasswordTextBox.Text = ""
UsernameTextBox.Text = ""
End If
End While
Else
MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'Clear all fields
PasswordTextBox.Text = ""
UsernameTextBox.Text = ""
End If
lrd.Close()
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close()
End Try
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
End Class