防止重复输入用户名

时间:2015-09-18 01:58:05

标签: duplicates multiple-entries

Private Sub APMSubmitBTN_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理APMSubmitBTN.Click

    con = New OleDbConnection(cs)
    con.Open()

    If txt_appmngr_username.Text = "" Then
        MessageBox.Show("Please enter Employee Name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txt_appmngr_username.Focus()
        Exit Sub
    End If

    If txt_appmngr_password.Text = "" Then
        MessageBox.Show("Please enter password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txt_appmngr_password.Focus()
        Exit Sub
    End If

    If chck_box_admin.Checked Then
        Dim sqlCheck As New OleDbCommand
        Dim cb As String = "INSERT INTO Admin_Account([Admin_User],[Admin_Pass]) values (@a1,@a2)"
        cmd = New OleDbCommand(cb, con)
        'CHECKING WETHER THE USERNAME IS ALREADY TAKEN BY OTHER ADMINISTRATOR'
        sqlCheck.CommandText = "SELECT count(*) FROM Admin_Account WHERE Admin_User = @1"
        sqlCheck.Connection = con
        cmd.Parameters.AddWithValue("@a1", txt_appmngr_username.Text)
        Dim oleRdr As OleDbDataReader = sqlCheck.ExecuteReader

        If oleRdr.HasRows = True Then
            oleRdr.Read()
            If oleRdr.Item(0) = 0 Then
                MessageBox.Show("Username is available", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Username is unavailable", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

        cmd.Parameters.AddWithValue("@a2", txt_appmngr_password.Text)

        cmd.ExecuteReader()

        MessageBox.Show("Successfully registered as Admin", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)

        con.Close()

    ElseIf chck_box_user.Checked Then
        Dim sqlCheck As New OleDbCommand
        Dim cb As String = "INSERT INTO Staff_Account([Staff_User],[Staff_Pass]) values (@s1,@s2)"
        cmd = New OleDbCommand(cb, con)
        'CHECKING WETHER THE USERNAME IS ALREADY TAKEN BY OTHER STAFF'
        sqlCheck.CommandText = "SELECT count(*) FROM Staff_Account WHERE Staff_User = @s1"
        sqlCheck.Connection = con
        cmd.Parameters.AddWithValue("@s1", txt_appmngr_username.Text)
        Dim oleRdr1 As OleDbDataReader = sqlCheck.ExecuteReader

        If oleRdr1.HasRows = True Then
            oleRdr1.Read()
            If oleRdr1.Item(0) = 0 Then
                MessageBox.Show("Username is available", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Username is unavailable", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If


        cmd.Parameters.AddWithValue("@s2", txt_appmngr_password.Text)

        cmd.ExecuteReader()

        MessageBox.Show("Successfully registered as Staff", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)

        con.Close()
    End If


End Sub

我在用户和管理员帐户的Dim oleRdr作为OleDbDataReader = sqlCheck.ExecuteReader的行上得到错误。当我运行程序时。

错误表示“没有给出一个或多个必需参数的值。”

0 个答案:

没有答案