简体   繁体   中英

Check if data already exists in database

Can anyone help me to check whether the data already exists in the database by displaying message in red color if the data already exists in the database.

I want to check if the StaffID already exists in the database, if it does, the user cannot create a new profile using that same staffID .

But I do not know how to do it. Can anyone help me to do the code to check whether the staffID already exists in the database or not?

Your help is much appreciated.

This is my code:

<table id="tblBasicInfo">
  <tr>
     <td style="width: 50%">
        <div class="form-group">
           <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span>Staff ID</label>
            <div class="col-sm-8">
                <asp:DropDownList ID="ddlStaffID" class="chosen-select form-control col-sm-9" Width="100%" data-placeholder="Choose StaffID" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlStaffID_SelectedIndexChanged"></asp:DropDownList>
            </div>
         </div>
      </td><tr/>
  <tr>
     <td style="width:50%">
       <div class="form-group">
          <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Full Name</label>
            <div class="col-sm-8">
             <asp:TextBox class="form-control" placeholder="Enter your First Name" id="txtFirstName" runat="server"></asp:TextBox>
            </div>
       </div>
     </td>
     <td style="width: 50%">                                            
      <div class="form-group">
      <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Last Name</label>
        <div class="col-sm-8">
          <asp:TextBox class="form-control" placeholder="Enter your Last Name" id="txtLastName" runat="server"></asp:TextBox>
          <span class="help-inline col-xs-12 col-sm-7" />
        </div>
     </div>
    </td>
 </tr>
 <tr>
   <td style="width:50%">
     <div class="form-group">
       <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Email</label>
        <div class="col-sm-8">
         <asp:TextBox class="form-control" placeholder="Enter your Email Address" id="txtEmail" runat="server"></asp:TextBox>
         <span class="help-inline col-xs-12 col-sm-7" />
        </div>
       </div>
   </td>
   <td style="width:50%">
     <div class="form-group">
     <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Mobile Phone</label>
     <div class="col-sm-8">
      <asp:TextBox class="form-control" placeholder="Enter Mobile Phone Number" id="txtMobilePhone" runat="server"></asp:TextBox>
      <span class="help-inline col-xs-12 col-sm-7" />
      </div>
     </div>
   </td>
 </tr>

Aspx code

Protected Sub ddlStaffID_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Try
        ErrMsg = "LoadDataGrid "
        attPage.SQLQuery = DC.Data_TechnicalProfile("1001", ddlStaffID.SelectedItem.Value)
        DS = DA.GetSQLDataset(attPage.SQLQuery)
        If DS IsNot Nothing AndAlso DS.Tables(0).Rows.Count > 0 Then
            txtFirstName.Text = DS.Tables(0).Rows(0)("ParticipantName").ToString
            txtEmail.Text = DS.Tables(0).Rows(0)("Email").ToString
            txtMobilePhone.Text = DS.Tables(0).Rows(0)("ContactNo").ToString
        End If
    Catch ex As Exception
        attPage.ErrorMessage = DA.GetErrorMessage(1, System.Reflection.MethodBase.GetCurrentMethod.Name.ToString, ErrMsg, ex.Message.ToString, attPage.ActionPage)
        ShowError(attPage.ErrorHeader, attPage.ErrorMessage)
    End Try
End Sub

Sub LoadDropDownList()
    Try

        attPage.SQLQuery = DC.Data_TechnicalResource("2")
        DS = DA.GetSQLDataset(attPage.SQLQuery)
        attPage.ErrorMessage = "SearchStaffID "
        ddlStaffID.DataTextField = "StaffID"
        ddlStaffID.DataValueField = "ID"
        ddlStaffID.DataSource = DS.Tables(5)
        ddlStaffID.DataBind()
        ddlStaffID.Items.Insert(0, "")



    Catch ex As Exception
        attPage.ErrorMessage = DA.GetErrorMessage(1, System.Reflection.MethodBase.GetCurrentMethod.Name.ToString, ErrMsg, ex.Message.ToString, attPage.ActionPage)
        ShowError(attPage.ErrorHeader, attPage.ErrorMessage)
    End Try
End Sub

SQL Query

IF @Action=101
BEGIN
    SELECT DISTINCT StaffID AS [StaffID], ParticipantID AS [ID], ParticipantName AS [Name]
    FROM mstUser
    WHERE StatusID = '1' AND GroupID = '12'
    ORDER BY StaffID
END

Assuming StatusID is the primary key of mstUser If all you need is to check the existence of a record then...

Private Sub OPCode2()
    Dim count As Integer
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("Select Count(*) From mstUser Where StatusID = @StatusID", cn)
            cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = ddlStaffID.SelectedItem.Value
            cn.Open()
            count = CInt(cmd.ExecuteScalar())
        End Using
    End Using
    If count > 0 Then 'Record exists
        'You red alert code here.
        'If showing the alert is the problem then that is another question
    End If
End Sub

Code as per @marc_s using If Exists.

Private Sub OPCode3()
    Dim Exists As Boolean
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("If Exists (Select * From mstUser Where StatusID = @StatusID) Select 1 Else Select 0;", cn)
            cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = ddlStaffID.SelectedItem.Value
            cn.Open()
            Exists = CBool(cmd.ExecuteScalar())
        End Using
    End Using
    If Exists Then 'Record exists
        'You red alert code here.
        'If showing the alert is the problem then that is another question
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM