Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

vb.net - Object reference not set to an instance of an object in con.State = ConnectionState.Closed

Public Class LoginForm
    Dim con As OleDbConnection
    Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;"
    Dim dbSource As String = "Data Source=C:UsersWindows10DesktopdbInventory.accdb;"

    Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        Me.Hide()
        RegistrationForm.Show()
    End Sub
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        If txtUserName.Text = Nothing Or txtPassword.Text = Nothing Then
            MessageBox.Show("Please enter correct username or password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        Using login As New OleDbCommand("SELECT COUNT(*) FROM tbl_EmployeesInfo WHERE [UserName] = @UserName AND [Password] = @Password", con)
            login.Parameters.AddWithValue("@UserName", OleDbType.VarChar).Value = txtUserName.Text.Trim
            login.Parameters.AddWithValue("@Password", OleDbType.VarChar).Value = txtPassword.Text.Trim

            Dim logincount = Convert.ToInt32(login.ExecuteScalar())
            If logincount > 0 Then
                Me.Hide()
                InventoryForm.Show()
            Else
                MessageBox.Show("UserName or Password is wrong. Try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtUserName.Clear()
                txtPassword.Clear()
            End If
        End Using
        con.Close()
    End Sub
End Class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Get rid of the Form level con. Database objects need to be local to the method where they are used so the can be disposed. You should never have to check connection state.

Glad to see the use of Using blocks but your connection needs to be in the Using too. Note the New keyword. Note the comma at the end of the first Using line. This means the Using continues and includes the command.

You have confused .AddWithValue and .Add. It is the .Add method that takes the parameter name and the datatype.

Don't show message boxes or do other things with the connection open. After all the user could have gone to lunch and you are tying up a connection. Declare logincount outside the Using block so it can be seen after End Using. Connections are precious resources. Open the connection directly before the .Execute... and close with the End Using as soon as possible.

Lastly, passwords should never be stored as plain text. This is beyond the scope of this question but look into encryption.

Dim conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersWindows10DesktopdbInventory.accdb;"

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    If txtUserName.Text = Nothing Or txtPassword.Text = Nothing Then
        MessageBox.Show("Please enter correct username or password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    Dim logincount As Integer
    Using con As New OleDbConnection(conString),
        login As New OleDbCommand("SELECT COUNT(*) FROM tbl_EmployeesInfo WHERE [UserName] = @UserName AND [Password] = @Password", con)
        login.Parameters.Add("@UserName", OleDbType.VarChar).Value = txtUserName.Text.Trim
        login.Parameters.Add("@Password", OleDbType.VarChar).Value = txtPassword.Text.Trim
        con.Open()
        logincount = CInt(login.ExecuteScalar())
    End Using
    If logincount > 0 Then
        Me.Hide()
        InventoryForm.Show()
    Else
        MessageBox.Show("UserName or Password is wrong. Try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtUserName.Clear()
        txtPassword.Clear()
    End If
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...