1

誰かが次のコードを分析して助けてくれれば幸いです。検証されるのは最初の条件だけですが、staffId が正しくなくてもデータを保存できます。

Try
    If rbnMale.Checked = True Then
        Sex = "M"
    Else
        Sex = "F"
    End If
    photo = "k"
    If txtStaffID.Text.Length < 6 Or txtStaffID.Text.Length > 6 Then
        lblStError.Text = "Please StaffID is not a six-digit integer"
        lblErrorMsges.Text = "At least one data validation error occured on this form. Please see the specific error messages."
        If txtName.Text.Length > 25 Then
            lblNameErr.Text = " Name should not exceed 25 characters"
            lblErrorMsges.Text = "At least one data validation error occured on this form. Please see the specific error messages."
                Else
        qry = "Insert into StaffDetails.Staff(StaffID,EmployeeName,SSN,Day,Month,Year,Sex,Qualifications,Rank,Responsibility,ApDay,ApMonth,ApYear,RegNumber,Phone,Email,Status,Photo)values(@StaffID,@EmployeeName,@SSN,@Day,@Month,@Year,@Sex,@Qualifications,@Rank,@Responsibility,@ApDay,@ApMonth,@ApYear,@RegNumber,@Phone,@Email,@Status,@Photo)"
        cmd = New SqlCommand(qry, cn)
        cmd.Parameters.Add(New SqlParameter("@StaffID", Val(txtStaffID.Text)))
        cmd.Parameters.Add(New SqlParameter("@EmployeeName", txtName.Text))
        cmd.Parameters.Add(New SqlParameter("@SSN", txtSsNumber.Text))
        cmd.Parameters.Add(New SqlParameter("@Day", cboDay.Text))
        cmd.Parameters.Add(New SqlParameter("@Month", cboMonth.Text))
        cmd.Parameters.Add(New SqlParameter("@Year", txtYear.Text))
        cmd.Parameters.Add(New SqlParameter("@Sex", Sex))
        cmd.Parameters.Add(New SqlParameter("@Qualifications", txtQualifications.Text))
        cmd.Parameters.Add(New SqlParameter("@Rank", txtRank.Text))
        cmd.Parameters.Add(New SqlParameter("@Responsibility", txtRespons.Text))
        cmd.Parameters.Add(New SqlParameter("@ApDay", cboApDay.Text))
        cmd.Parameters.Add(New SqlParameter("@ApMonth", cboApMonth.Text))
        cmd.Parameters.Add(New SqlParameter("@ApYear", txtApYear.Text))
        cmd.Parameters.Add(New SqlParameter("@RegNumber", txtRegdNum.Text))
        cmd.Parameters.Add(New SqlParameter("@Phone", txtPhone.Text))
        cmd.Parameters.Add(New SqlParameter("@Email", txtEmail.Text))
        cmd.Parameters.Add(New SqlParameter("@Status", cboStatus.Text))
        cmd.Parameters.Add(New SqlParameter("@Photo", img))
        cmd.ExecuteNonQuery()
        MessageBox.Show("Record successfully saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnSave.Enabled = False
    End If
End If
    Showgrid()
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
4

1 に答える 1

2

問題は、ネストされた If ステートメントがあるため、2 番目の条件を引き続きチェックし、合格した場合でもレコードを保存することです。エラーを示すブール値フラグを使用して、このようなことを行います。

このようなもの:

Dim bError As Boolean
Try
    If rbnMale.Checked = True Then
        Sex = "M"
    Else
        Sex = "F"
    End If
    photo = "k"
    If txtStaffID.Text.Length < 6 Or txtStaffID.Text.Length > 6 Then
        lblStError.Text = "Please StaffID is not a six-digit integer"
        lblErrorMsges.Text = "At least one data validation error occured on this form. Please see the specific error messages."
        bError = True
    End If

    If txtName.Text.Length > 25 Then
        lblNameErr.Text = " Name should not exceed 25 characters"
        lblErrorMsges.Text = "At least one data validation error occured on this form. Please see the specific error messages."
        bError = True
    End If

    If Not bError Then
        qry = "Insert into StaffDetails.Staff(StaffID,EmployeeName,SSN,Day,Month,Year,Sex,Qualifications,Rank,Responsibility,ApDay,ApMonth,ApYear,RegNumber,Phone,Email,Status,Photo)values(@StaffID,@EmployeeName,@SSN,@Day,@Month,@Year,@Sex,@Qualifications,@Rank,@Responsibility,@ApDay,@ApMonth,@ApYear,@RegNumber,@Phone,@Email,@Status,@Photo)"
        cmd = New SqlCommand(qry, cn)
        cmd.Parameters.Add(New SqlParameter("@StaffID", Val(txtStaffID.Text)))
        cmd.Parameters.Add(New SqlParameter("@EmployeeName", txtName.Text))
        cmd.Parameters.Add(New SqlParameter("@SSN", txtSsNumber.Text))
        cmd.Parameters.Add(New SqlParameter("@Day", cboDay.Text))
        cmd.Parameters.Add(New SqlParameter("@Month", cboMonth.Text))
        cmd.Parameters.Add(New SqlParameter("@Year", txtYear.Text))
        cmd.Parameters.Add(New SqlParameter("@Sex", Sex))
        cmd.Parameters.Add(New SqlParameter("@Qualifications", txtQualifications.Text))
        cmd.Parameters.Add(New SqlParameter("@Rank", txtRank.Text))
        cmd.Parameters.Add(New SqlParameter("@Responsibility", txtRespons.Text))
        cmd.Parameters.Add(New SqlParameter("@ApDay", cboApDay.Text))
        cmd.Parameters.Add(New SqlParameter("@ApMonth", cboApMonth.Text))
        cmd.Parameters.Add(New SqlParameter("@ApYear", txtApYear.Text))
        cmd.Parameters.Add(New SqlParameter("@RegNumber", txtRegdNum.Text))
        cmd.Parameters.Add(New SqlParameter("@Phone", txtPhone.Text))
        cmd.Parameters.Add(New SqlParameter("@Email", txtEmail.Text))
        cmd.Parameters.Add(New SqlParameter("@Status", cboStatus.Text))
        cmd.Parameters.Add(New SqlParameter("@Photo", img))
        cmd.ExecuteNonQuery()
        MessageBox.Show("Record successfully saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnSave.Enabled = False
    End If
    Showgrid()
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
于 2012-11-24T21:49:50.207 に答える