-1

以下代码阻止我保存新记录。如果有人可以为我编辑它,我会很高兴。无论记录是否存在,都会显示“记录存在”消息。

尝试

        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

            cmd2 = New SqlCommand("select count(*) from ProgramDetails.Subjects where IdNumber = " & txtIDNumber.Text & " ", cn)
            queryResult = cmd2.ExecuteScalar
            If queryResult = 0 Then
                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
            MessageBox.Show("Record already exists", "Existing record", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
4

2 回答 2

0

您错过了条件 if 语句的另一半。

     If queryResult = 0 Then
            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()
        Else

MessageBox.Show("记录已存在", "已有记录", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

那应该给你你想要的。

于 2013-02-14T18:52:17.770 回答
0
 Dim cmd2 As New SqlCommand
    Dim queryResult As Integer
     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
                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)

                btnClear.Focus()
                showgrid()
            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
于 2013-02-25T16:07:10.083 回答