ExecuteReader: Connection property has not been initialized

910 views Asked by At

Can you explain why I am getting this error?

ExecuteReader: Connection property has not been initialized

Protected Sub btnLogin_Click(ByVal sender As Object, 
               ByVal e As System.EventArgs) Handles btnLogin.Click
    Dim name = txtUserName.Text.Trim
    Dim Pass = txtPassword.Text.Trim

    Try

        Dim conn As SqlConnection
        conn = DataBaseFunc.OpenConnection()

        Dim sqlSt As String = "select * FROM AdminLog WHERE AdminName = '" & name & "' AND AdminPassword = '" & Pass & "'"
        Dim SelCmd As SqlCommand = New SqlCommand(sqlSt, conn)

        Dim RrecSet As SqlDataReader

        RrecSet = SelCmd.ExecuteReader()

        If RrecSet.Read() Then
            Session("Admin") = RrecSet("AdminName")
            Response.Redirect("~/AdminPages/AdminHome.aspx")
        Else
            lblMsg.Text = "<p>Rong Username or Password</p>"
            txtUserName.Text = ""
            txtPassword.Text = ""
            txtUserName.Focus()

        End If
    Catch ex As Exception
        lblMsg.Text = ex.Message
    End Try
End Sub

DataBaseFunc

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic


Public Class DataBaseFunc
    Public Shared Sub EnsureOpenConnection(ByRef conn As SqlConnection)
        Try
            If conn.State <> Data.ConnectionState.Open Then
                conn.Open()
            End If
        Catch ex As Exception
        End Try
    End Sub


    Public Shared Function OpenConnection() As SqlConnection
        Try
            Dim ConnString As String = "server =.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BloodDb.mdf;Integrated Security=True;User Instance=True"
            Dim conn As New SqlConnection(ConnString)
            If conn.State <> Data.ConnectionState.Open Then
                conn.Open()
            End If
            Return conn
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Class
1

There are 1 answers

4
Hoh On

Change your DataBaseFunc code to something like this, and MsgBox might give you a better understanding of error.
Having Try/Catch with no actual code inside (like MsgBox(ex.toString) or println(ex.toString) is useless. After you do and try this, report as with further information.

Public Class DataBaseFunc
        Public Shared Sub EnsureOpenConnection(ByRef conn As SqlConnection)
            Try
                If conn.State <> Data.ConnectionState.Open Then
                    conn.Open()
                End If
            Catch ex As Exception
               MsgBox(ex.ToString)
            End Try
        End Sub


        Public Shared Function OpenConnection() As SqlConnection
            Try
                Dim ConnString As String = "server =.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BloodDb.mdf;Integrated Security=True;User Instance=True"
                Dim conn As New SqlConnection(ConnString)
                If conn.State <> Data.ConnectionState.Open Then
                    conn.Open()
                End If
                Return conn
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Function