你能解释一下我收到此错误的原因吗?
ExecuteReader:Connection属性尚未初始化
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
答案 0 :(得分:0)
将您的DataBaseFunc
代码更改为此类代码,MsgBox
可能会让您更好地了解错误。
在没有实际代码的情况下尝试/捕获(例如MsgBox(ex.toString)
或println(ex.toString)
是没用的。在您尝试此操作之后,请使用更多信息进行报告。
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