誰かが次のコードを分析して助けてくれれば幸いです。検証されるのは最初の条件だけですが、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