简体   繁体   English

VB.net COMExeption未处理

[英]VB.net COMExeption was unhandled

I am creating a user login system using vb.net and MS access. 我正在使用vb.net和MS Access创建用户登录系统。 I am unsure what is going wrong with my system and I receive the error message "Item cannot be found in the collection corresponding to the requested name or ordinal" The error is coming up in the section "User.Find(Username)" on the first line of the DO loop. 我不确定系统出了什么问题,并且收到错误消息“在与所请求的名称或序数相对应的集合中找不到项目”。错误出现在“ User.Find(Username)”部分DO循环的第一行。 Here is my code: 这是我的代码:

Public Class Login

Dim LoginError As String ' This will tell the user what is wrong with his login

Public Function Login()
    Dim DBConn As New ADODB.Connection ' This is how we tell visual studio 
    'how to connect to our database 

    Dim User As New ADODB.Recordset 'We pass our argument through our recordset

    Dim Username As String 'This will be our "Query"
    Dim strUserDB As String 'This get sets to the email field in our database.
    Dim strPassDB As String 'Same as above just for the password

    Dim blnUserFound As Boolean 'I will be using a "DO" loop so I will use 
    'this as my condition

    DBConn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
                 "Data Source = '" & Application.StartupPath & "\UserDetails2000.mdb'")
    'The inverted comas in the dataOuce statement as itt keeps the location of your 
    'file as one string.

    User.Open("tblUserDetails", DBConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
    'This is my table           'This is my connection         'These are some settings

    blnUserFound = False
    Login = False
    Username = "User = '" & txtEmail.Text & "'" 'This tells the database to find the email field 
    'Equivilent to what was entered in the textbox

    Do
        User.Find(Username) 'This is the full statement that sends my 'Query' to the record set
        If User.BOF = False And User.EOF = False Then
            'BOF = Begining of file, EOF = End of file, it tests whether the database has 
            'reached its sentinal value, if it hasent then the username has been found, If it has,
            'the username has been found.

            strUserDB = User.Fields("Email").Value.ToString
            '"Email" is my table field. I am setting strUserDB to the username field of my table
            strPassDB = User.Fields("Password").Value.ToString

            If strUserDB <> txtEmail.Text Then
                User.MoveNext()
                'This IF statement handles different CASE usernames, Example, admin and AdMiN
                'We use this if statement to differentiate between different CASE letters
            Else
                blnUserFound = True
                If strPassDB = txtPassword.Text Then
                    User.Close()
                    DBConn.Close()
                    Return True
                Else
                    LoginError = "Invalid Password"
                    User.Close()
                    DBConn.Close()
                    Return False
                End If
            End If
        Else
            LoginError = "Invalid Username"
            User.Close()
            DBConn.Close()
            Return False

        End If
    Loop Until blnUserFound = True
    LoginError = "Invalid Username"
    User.Close()
    DBConn.Close()
    Return False


End Function

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    If Login() = True Then
        MessageBox.Show("Login Succesful", "Login Status")
    Else
        MessageBox.Show(LoginError, "Login Status")
    End If
End Sub

End Class 末级

Verify that tblUserDetails contains a column named User . 验证tblUserDetails是否包含名为User的列。

Maybe User is also a reserved keyword in Access so try setting Username as: 也许User也是Access中的保留关键字 ,所以请尝试将Username设置为:

Username = "[User] = '" & txtEmail.Text & "'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM