如果有人能够分析以下代码并帮助我,我会更高兴。它只是第一个经过验证的条件,但即使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
答案 0 :(得分:2)
问题是你有一个嵌套的If语句,所以它仍会检查第二个条件,如果它仍然通过,则保存你的Record。我会用布尔标志做这样的事情来表示错误。
这样的事情:
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