注册表单不会工作 - 代码中的错误 - dbCommand.ExecuteReader()

时间:2016-01-15 15:51:06

标签: vb.net visual-studio visual-studio-2012

我目前正在尝试在视觉工作室中开发一个注册系统,但由于某种原因它只是不起作用,因为它会显示以下错误信息。

在第Dim read As OleDb.OleDbDataReader = cmd.ExecuteReader()行,它说:

  

未处理的类型' System.Data.OleDb.OleDbException'发生在System.Data.dl。

请帮助我,因为我已经做了一切正确的事情,但是我只是不明白为什么注册系统不起作用。

'Enables the use of the OleDb classes.
Imports System
Imports System.Data.OleDb

Imports System.Net.Mail

Public Class Register
    'Determines the connection and the location within the users file system.
    Dim myConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Environment.CurrentDirectory & "\BloodBank.mdb"



    Private Sub Register_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'This line of code loads data into the 'BloodBankDataSet.User' table. You can move, or remove it, as needed.
        Me.UserTableAdapter.Fill(Me.BloodBankDataSet1.User)
        'BindingSource - Encloses the data source for a form, in this case Register.
        ' AddNew, adds a new record.
        Me.UserBindingSource.AddNew()
    End Sub

    Private Sub Reg_Click(sender As Object, e As EventArgs) Handles Reg.Click
        'creates the connection between the project and the database ensuring that any changes made will be updated within the database.
        Dim con As New OleDbConnection(myConnString)
        'creates a second connection between the project and the database ensuring that any changes made will be updated within the database.
        Dim con2 As New OleDbConnection(myConnString)
        'Opens the connections.
        con.Open()
        con2.Open()
        'Creates a SQL command which...
        'Selects the staff details dependendent on the value inputted by the user.
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT StaffID, FirstName, Surname FROM [Staff] WHERE (FirstName = '" & Me.FirstNameTextBox.Text & "') AND (Surname = '" & Me.SurnameTextBox.Text & "') ", con)
        'sends the command above to the connection.
        Dim read As OleDb.OleDbDataReader = cmd.ExecuteReader()
        'Creates a SQL command which...
        'Selects the User ID and Staff ID from user dependent on the value inputted by the user.
        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT UserID, StaffID FROM [User] WHERE StaffID = '" & Me.StaffIDTextBox.Text & "' ", con2)
        'sends the command above to the connection.
        Dim read2 As OleDb.OleDbDataReader = cmd2.ExecuteReader()

        'If there is no text within the staff ID text box...
        If String.IsNullOrEmpty(StaffIDTextBox.Text) Then
            'A message will appear telling the user to enter their staff ID.
            MsgBox("Please enter StaffID")
            'This will focus the user on the staff ID text box.
            'Me. means the current form in use.
            Me.StaffIDTextBox.Focus()

            'If there is no text within the FirstName text box...
        ElseIf String.IsNullOrEmpty(FirstNameTextBox.Text) Then
            'A message will appear telling the user to enter their first name.
            MsgBox("Please enter First Name")
            'This will focus the user on the firstname text box.
            Me.FirstNameTextBox.Focus()

            'If there is no text within the Surname text box...
        ElseIf String.IsNullOrEmpty(SurnameTextBox.Text) Then
            'A message will appear telling the user to enter their surname.
            MsgBox("Please enter Surname")
            'This will focus the user on the surname text box.
            Me.SurnameTextBox.Focus()

            'If there is no text within the login ID text box...
        ElseIf String.IsNullOrEmpty(UserLoginTextBox.Text) Then
            'A message will appear telling the user to enter their login ID.
            MsgBox("Please Enter LoginID")
            'This will focus the user on the loginID text box.
            Me.UserLoginTextBox.Focus()

            'If there is no text within the user password text box...
        ElseIf String.IsNullOrEmpty(UserPasswordTextBox.Text) Then
            'A message will appear telling the user to enter their password.
            MsgBox("Please Enter Password")
            'This will focus the user on the password text box.
            Me.UserPasswordTextBox.Focus()


            'If there is no text within the user ID text box...
        ElseIf String.IsNullOrEmpty(EmailAddressTextBox.Text) Then
            'A message will appear telling the user to enter their email.
            MsgBox("Please Enter Email")
            'This will focus the user on the email text box.
            Me.EmailAddressTextBox.Focus()


            'If the items do not match up with that within the database then...
        ElseIf read.Read = False Then
            'A message box will appear informing of the failure of the registration and the reason why.
            MsgBox("You must be a staff member to create a User")
            'This will focus the user on the StaffID text box.
            Me.StaffIDTextBox.Focus()

            'If the items do match up with that within the database then...
        ElseIf read2.Read = True Then
            'Determines that the answer data type is a integer.
            Dim answer As Integer
            'creates a message box, which is assigned to answer with a question with a yes and no option 
            answer = MsgBox("There is already a User account with this Staff ID." & vbNewLine & "Have you forgotten your Password? ", vbYesNo + vbQuestion)
            'if the user selects the yes button...
            If answer = vbYes Then
                'They will be redirected to the retrieve password form.
                '.Show, opens the said form, in this case the retrieve password form.
                RetrievePass.Show()
                '.Close, closes the form, in this case it is the form currently in use.
                Me.Close()
            Else
                'Do nothing.
            End If

        ElseIf read2.Read = False And Me.StaffIDTextBox.Text = read.Item("StaffID") And Me.FirstNameTextBox.Text = read.Item("FirstName") And Me.SurnameTextBox.Text = read.Item("Surname") Then

        End If

        MsgBox("Thank you for creating a User." & vbNewLine & "We have sent your User details to your Email")
        'Security Level
        'Makes a certain feild in a specified table usable or not usable for the user.
        HomeF.ButForm.Enabled = True
        HomeF.ButUse.Enabled = False
        HomeF.Button2.Enabled = True

        'Me. means the current form in use.
        '.Validate checks that the information is correct and every aspect has been filled in.
        Me.Validate()
        'EndEdit, applies the changes to the data source.
        Me.UserBindingSource.EndEdit()
        'Updates the specified table within the specified dataset.
        Me.UserTableAdapter.Update(Me.BloodBankDataSet1)

        Me.UserLogin_L.Text = Me.UserLoginTextBox.Text

        '.Validate checks that the information is correct and every aspect has been filled in.
        Me.Validate()
        'EndEdit, applies the changes to the data source.


        Try
            Dim Email As String = Me.EmailAddressTextBox.Text
            Dim name As String = Me.FirstNameTextBox.Text
            Dim userpassword As String = Me.UserPasswordTextBox.Text
            Dim LoginID As String = Me.UserLoginTextBox.Text
            Dim StaffID As Integer = Me.StaffIDTextBox.Text

            Dim Smtp_Server As New SmtpClient("smtp.gmail.com")
            Dim e_mail As New MailMessage
            Smtp_Server.UseDefaultCredentials = False


            'Have to put in the email account details to enable the use of sending emails via the account.
            Smtp_Server.Credentials = New Net.NetworkCredential("blood4all1@gmail.com", "Blood1234")
            Smtp_Server.Port = 587
            Smtp_Server.EnableSsl = True
            'smtp(Simple Mail Transfer Protocol)

            e_mail.From = New MailAddress("blood4all@outlook.com")
            e_mail.To.Add(Email)
            e_mail.Subject = ("New User: Blood4All Blood Bank")
            e_mail.IsBodyHtml = False
            e_mail.Priority = MailPriority.High
            e_mail.Body = ("Dear " & name & "" & vbNewLine & "" & vbNewLine & "A New User Account was created with Your StaffID: " & StaffID & ". If the account was not created by You Contact Blood4All immediately." & vbNewLine & "Your LoginID is: " & LoginID & "" & vbNewLine & "Your Password is: " & userpassword & "" & vbNewLine & "" & vbNewLine & "Regards" & vbNewLine & "Blood4All Blood Bank")

            Smtp_Server.Send(e_mail)
            Close()
            HomeF.Show()


        Catch error_t As Exception
            MsgBox("Error when Sending Your Email: Email Address does not Exist or There is No Network Connection")
            Close()
            HomeF.Show()

        End Try

        End
        'Closes the four objects.
        read.Close()
        read2.Close()
        con.Close()
        con2.Close()
    End Sub

0 个答案:

没有答案