I need to be pointed in the right direction....
I have a Windows Form to insert a record into an "INSTRUCTORS" table.
It works great but I need a MessageBox to appear when a Social Security Number is already in use. I also need it to display the name of the Instructor the Social Security Number is currently assigned.
I'm posting the code I have so far below...
I marked the Messagebox in RED.
Question: Should I do this type of validation at the field level or is this acceptable? Any suggestions?
Thanks
Private Sub InsertNewInstructor()
Dim cn As New SqlConnection("Data Source=BH01WF54;Initial Catalog=HUNTER_SAFETY_PROGRAM;Integrated Security=SSPI")
Dim cmd As New SqlCommand()
cmd.Parameters.AddWithValue("@InstFName", FirstName.Text)
cmd.Parameters.AddWithValue("@InstMName", MiddleName.Text)
cmd.Parameters.AddWithValue("@InstLName", LastName.Text)
cmd.Parameters.AddWithValue("@InstSuffix", Suffix.Text)
cmd.Parameters.AddWithValue("@InstSSN", SocialSecurityNumber.Text)
cmd.Parameters.AddWithValue("@InstAddress", Address.Text)
cmd.Parameters.AddWithValue("@InstCity", City.Text)
cmd.Parameters.AddWithValue("@InstState", State.SelectedItem)
cmd.Parameters.AddWithValue("@InstZipCode", ZipCode.Text)
cmd.Parameters.AddWithValue("@InstWorkCountyCode", WorkCounty.SelectedValue)
cmd.Parameters.AddWithValue("@InstHomePhone", HomePhone.Text)
cmd.Parameters.AddWithValue("@InstWorkPhone", WorkPhone.Text)
cmd.Parameters.AddWithValue("@InstEmail", Email.Text)
cmd.Parameters.AddWithValue("@InstDOB", DateOfBirth.Value)
cmd.Parameters.AddWithValue("@InstComment", Comment.Text)
cmd.Parameters.AddWithValue("@InstTWRAEmp", TWRAEmp.CheckState)
cmd.Parameters.AddWithValue("@InstBegCtDate", CertificationDate.Value)
cmd.Connection = cn
cmd.CommandText = "INSERT_NEW_INSTRUCTOR"
cmd.CommandType = CommandType.StoredProcedure
cn.Open()
Dim Affected As Integer = cmd.ExecuteNonQuery()
If Affected = 1 Then
MessageBox.Show(Affected & " ACCOUNT SUBMITTED")
ElseIf Affected = -1 Then
MessageBox.Show("SOCIAL SECURITY NUMBER ALREADY IN USE BY: ", "INVALID ENTRY", MessageBoxButtons.OK, MessageBoxIcon.Error)
cn.Close()
cn.Dispose()
End If
End Sub
I have a Windows Form to insert a record into an "INSTRUCTORS" table.
It works great but I need a MessageBox to appear when a Social Security Number is already in use. I also need it to display the name of the Instructor the Social Security Number is currently assigned.
I'm posting the code I have so far below...
I marked the Messagebox in RED.
Question: Should I do this type of validation at the field level or is this acceptable? Any suggestions?
Thanks
Private Sub InsertNewInstructor()
Dim cn As New SqlConnection("Data Source=BH01WF54;Initial Catalog=HUNTER_SAFETY_PROGRAM;Integrated Security=SSPI")
Dim cmd As New SqlCommand()
cmd.Parameters.AddWithValue("@InstFName", FirstName.Text)
cmd.Parameters.AddWithValue("@InstMName", MiddleName.Text)
cmd.Parameters.AddWithValue("@InstLName", LastName.Text)
cmd.Parameters.AddWithValue("@InstSuffix", Suffix.Text)
cmd.Parameters.AddWithValue("@InstSSN", SocialSecurityNumber.Text)
cmd.Parameters.AddWithValue("@InstAddress", Address.Text)
cmd.Parameters.AddWithValue("@InstCity", City.Text)
cmd.Parameters.AddWithValue("@InstState", State.SelectedItem)
cmd.Parameters.AddWithValue("@InstZipCode", ZipCode.Text)
cmd.Parameters.AddWithValue("@InstWorkCountyCode", WorkCounty.SelectedValue)
cmd.Parameters.AddWithValue("@InstHomePhone", HomePhone.Text)
cmd.Parameters.AddWithValue("@InstWorkPhone", WorkPhone.Text)
cmd.Parameters.AddWithValue("@InstEmail", Email.Text)
cmd.Parameters.AddWithValue("@InstDOB", DateOfBirth.Value)
cmd.Parameters.AddWithValue("@InstComment", Comment.Text)
cmd.Parameters.AddWithValue("@InstTWRAEmp", TWRAEmp.CheckState)
cmd.Parameters.AddWithValue("@InstBegCtDate", CertificationDate.Value)
cmd.Connection = cn
cmd.CommandText = "INSERT_NEW_INSTRUCTOR"
cmd.CommandType = CommandType.StoredProcedure
cn.Open()
Dim Affected As Integer = cmd.ExecuteNonQuery()
If Affected = 1 Then
MessageBox.Show(Affected & " ACCOUNT SUBMITTED")
ElseIf Affected = -1 Then
MessageBox.Show("SOCIAL SECURITY NUMBER ALREADY IN USE BY: ", "INVALID ENTRY", MessageBoxButtons.OK, MessageBoxIcon.Error)
cn.Close()
cn.Dispose()
End If
End Sub