0

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Since the msg clearly indicates a Connection issue, the code for `DataBaseFunc.OpenConnection()` would be nice to see. Also, Parameterized queries would prevent SQL injection (look it up) and you ought not store Passwords in DBs (esp Admin!). – Ňɏssa Pøngjǣrdenlarp Jun 13 '15 at 13:59
  • what sort of change? – Michael Gregory Jun 13 '15 at 15:13
  • Get rid of the do-nothing Try/Catch in OpenConnection so that when something goes wrong you'll know about it. A Try/Catch is not supposed to hide problems from the developer – Ňɏssa Pøngjǣrdenlarp Jun 13 '15 at 16:03
  • is it possible for you to give me updated code? please? i am really not good at this. and i really dont want to screw up – Michael Gregory Jun 13 '15 at 16:12
  • 1
    You have to do *some* work yourself. If something is wrong with the connection string or something else fails, you will never know about it because of the empty Try/Catch; if it returns `Nothing` you can use `Nothing` as a valid connection!. I cant post an answer because no one knows for sure what the answer is because your code is not reporting the problem. Get rid of the Try/Catch and there is a good chance NET will give you an actionable exception message. – Ňɏssa Pøngjǣrdenlarp Jun 13 '15 at 16:25
  • so i should remove whole of try to catch code? can i contact you on whatsapp or something – Michael Gregory Jun 13 '15 at 16:29
  • 1
    remove `try`, `catch` , `return Nothing` and `End Try`, not the code inside the block that opens the connection. This may reveal the **real** problem which you can use to update/edit your post with the relevant info – Ňɏssa Pøngjǣrdenlarp Jun 13 '15 at 16:34

1 Answers1

0

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
Hoh
  • 1,196
  • 1
  • 12
  • 30