0

I'm getting this error: "ErrorExecuteReader: Connection property has not been initialized"

I can't figure out what the problem is.

Imports System.Boolean
Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim userID As String
        Dim password As String
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim reader As SqlDataReader

        userID = txtuser.Text
        password = txtpassword.Text
        txtuser.Text = Focus()
        txtpassword.Visible = "False"

        Try
            conn = New SqlConnection("Data Source=XXXXXX;Initial Catalog=XXXXXXUser ID=XXXXXX;Password=XXXXXX")
            cmd = New SqlCommand("Select user,password from userlog where user='" + userID + "' & password='" + password + "'")

            conn.Open()
            reader = cmd.ExecuteReader()

            conn.Close()    

            If (String.Compare(password, 123) = 0) Then
                MsgBox("Success")
            End If
        Catch ex As Exception
            MsgBox("Error" + ex.Message())
        End Try
    End Sub
End Class
4

1 に答える 1

4

コマンド オブジェクトの接続プロパティが設定されていません。次のようにコンストラクターに追加します。

cmd = New SqlCommand("...", conn)

または、次のように Connection プロパティを設定します。

cmd.Connection = conn

ちなみに、Visual Studio でデバッグしていた場合、このエラーは簡単にキャッチできるはずです。

于 2013-03-07T03:41:27.300 に答える