hi there,
i am having problem with my codes. im getting this error,
'Name 'reader' is not declared'. please help.

[B]reader = con2.ExecuteReader()[/B]

[B] reader.Read [/B]


Protected Function SiteSpecificAuthenticationMethod(ByVal UserName As String, ByVal Password As String) As Boolean

        Dim log_statement As String = "SELECT * FROM [user] where username='" & UserName & "' and password='" & Password & "'"

        Dim con2 As System.Data.SqlClient.SqlCommand
        Dim con1 As System.Data.SqlClient.SqlConnection

        con1 = New System.Data.SqlClient.SqlConnection(SqlDataSource1.ConnectionString)
        con1.Open()

        con2 = New System.Data.SqlClient.SqlCommand(log_statement, con1)
        [B]reader = con2.ExecuteReader()[/B]

        If[B] reader.Read [/B]Then

            Return True

        Else

            Return False

        End If

    End Function

Dani AI

Generated

The compile error "Name 'reader' is not declared" means there is no variable with that identifier in the current scope. correctly pointed toward a missing declaration; the original code attempted to use a data reader without declaring it first. 's example highlights the usual reader pattern (check rows and close), but a safer, simpler pattern for an existence check is to avoid a reader entirely and use a scalar query with parameterized SQL and proper resource disposal.

A compact, safer replacement uses a parameterized COUNT query inside Using blocks so connections and commands are always disposed:

Using cn As New System.Data.SqlClient.SqlConnection(connectionString)
    Using cmd As New System.Data.SqlClient.SqlCommand("SELECT COUNT(1) FROM [User] WHERE username=@u AND password=@p", cn)
        cmd.Parameters.Add("@u", System.Data.SqlDbType.NVarChar, 50).Value = UserName
        cmd.Parameters.Add("@p", System.Data.SqlDbType.NVarChar, 128).Value = Password
        cn.Open()
        Dim found As Integer = System.Convert.ToInt32(cmd.ExecuteScalar())
        Return (found > 0)
    End Using
End Using

Security and correctness notes based on the thread: avoid concatenating user input into SQL (the original is vulnerable to SQL injection). Do not store plaintext passwords — store salted, iterated hashes (PBKDF2/bcrypt/Argon2) and compare hashes rather than raw passwords. Prefer SELECT of a specific column or COUNT rather than SELECT * for existence checks. If a reader is still required, declare a reader variable in the same method scope and use Using or explicit Close/Dispose, and check whether any rows exist before accessing fields.

Acknowledgment: 's confirmation is noted. The immediate fixes are (1) declare the reader before use (if sticking with ExecuteReader), or (2) switch to the ExecuteScalar pattern above and implement parameterization and proper password hashing for production use.

Recommended Answers

All 4 Replies

Hi There

Pls. see the code that uses reader

You can assign data to sql reader

Public Function CheckRequest(ByVal response As String) As Boolean
        Dim reader As SqlDataReader
        Try
            reader = SqlHelper.ExecuteReader(objConnection, "CheckRequest", RequestName)
            If reader.HasRows Then
                reader.Close()
                objConnection.Close()
                Return True
            Else
                reader.Close()
                objConnection.Close()
                Return False
            End If

        Catch ex As Exception

            objConnection.Close()
        End Try

    End Function
reader = con2.ExecuteReader()

try this:

Dim reader As SqlDataReader
reader = con2.ExecuteReader()

or:

Dim reader As SqlDataReader = con2.ExecuteReader()

they are correct..

they are correct..

Have you solved your problem? if you have so flag this thread as solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.