'How do I Solve this message "There is already an open DataReader associated with this Command which must be closed first"

Imports System.Data.SqlClient

Public Class Login
    Dim lines() As String = IO.File.ReadAllLines(Application.StartupPath + "\startup.ini")
    Dim servername As String = lines(0)
    Dim password As String = lines(lines.Length - 1)
    Dim username As String = lines(lines.Length - 2)
    Dim dbname As String = lines(lines.Length - 3)
    Dim connection As New SqlConnection("Data Source='" & servername & "';Initial Catalog='" & dbname & "';User ID='" & username & "';Password='" & password & "'")

   

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If
        Dim cmd As New SqlCommand("Select * from logs where user=@user and password=@password ", connection)
        cmd.Parameters.AddWithValue("Password", PasswordTextBox.Text)
        cmd.Parameters.AddWithValue("user", UsernameTextBox.Text)

         Dim myreader As SqlDataReader = cmd.ExecuteReader
        If (myreader.Read()) Then
            myreader.Close()
            username_v = myreader("user")
            Home.Show()
            Me.Hide()
        Else
            MsgBox("Incorrect Username or Password")
        End If
End Sub

While Running this code, the following message will appear

There is already an open DataReader associated with this Command which must be closed first.


Solution 1:[1]

Imports System.Data.SqlClient
Imports BCrypt.Net ' Get this via NuGet

Public Class Login
    Dim lines() As String = IO.File.ReadAllLines(Application.StartupPath + "\startup.ini")
    Dim servername As String = lines(0)
    Dim password As String = lines(lines.Length - 1)
    Dim username As String = lines(lines.Length - 2)
    Dim dbname As String = lines(lines.Length - 3)
   
    ' I REALLY hope you do better at securing the DB connection password than this
    Dim cnString As String = $"Data Source='{servername}';Initial Catalog='{dbname}';User ID='{username}';Password='{password}'"   

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

        Using cn As new SqlConnection(cnString), _
              cmd As New SqlCommand("Select user, password from logs where user=@user ", cn)

        'I'm guessing at the parameter type here. You should use the actual value from the database
        cmd.Parameters.Add("@user", SqlDbType.NVarChar, 40).Value =  UsernameTextBox.Text

        Using myreader As SqlDataReader = cmd.ExecuteReader()
            If myreader.Read() AndAlso BCrypt.Verify(PasswordTextBox.Text, myreader("password")) Then               
                username_v = myreader("user")
                Home.Show()
                Me.Hide()
            Else
                MsgBox("Incorrect Username or Password")
            End If
        End Using
    End Using
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1