Please I'd be thankful if someone could very kindly edit the following code for me. Currintly, the code doesn't enable the user to save a new record; but, it's supposed to allow insertion of new record as well as prevent insertion of duplicate record.
Try
cmd2 = New SqlCommand("select count(*) from ProgramDetails.Subjects where IDNumber = '" & txtIDNumber.Text & "'", cn)
queryResult = cmd2.ExecuteScalar
If queryResult > 0 Then
If String.IsNullOrEmpty(subject_id) Or rbnCore.Checked = False And rdbElective.Checked = False Or String.IsNullOrEmpty(username) Or String.IsNullOrEmpty(password) Then
MessageBox.Show("One or more fields have not been left empty.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
txtSubjectCode.Focus()
'Else
If rbnCore.Checked = True Then
subject = "Core"
Else : subject = "Elective"
End If
If String.IsNullOrEmpty(subject_id) Or rbnCore.Checked = False And rdbElective.Checked = False Or String.IsNullOrEmpty(username) Or String.IsNullOrEmpty(password) Then
MessageBox.Show("One or more fields have not been left empty.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
txtSubjectCode.Focus()
qry = "Insert into ProgramDetails.Subjects(SubjectCode,SubjectName,SubjectType,UserID,Password)values(@SubjectCode,@SubjectName,@SubjectType,@UserID,@Password)"
cmd = New SqlCommand(qry, cn)
cmd.Parameters.Add(New SqlParameter("@SubjectCode", txtSubjectCode.Text))
cmd.Parameters.Add(New SqlParameter("@SubjectName", txtSubName.Text))
cmd.Parameters.Add(New SqlParameter("@SubjectType", subject))
cmd.Parameters.Add(New SqlParameter("@UserID", txtUserName.Text))
cmd.Parameters.Add(New SqlParameter("@Password", txtPassword.Text))
cmd.ExecuteNonQuery()
MessageBox.Show("Record successfully saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
btnSave.Enabled = False
btnClear.Focus()
showgrid()
End If
End If
Else
MessageBox.Show("Record already exists.", "Existing Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try