[英]Check if data already exists in database
如果數據已存在於數據庫中,任何人都可以通過以紅色顯示消息來幫助我檢查數據是否已存在於數據庫中。
我想檢查StaffID
已存在於數據庫中,如果存在,則用戶無法使用相同的staffID
創建新配置文件。
但我不知道該怎么做。 任何人都可以幫我做代碼來檢查數據庫中是否已經存在staffID
?
非常感謝您的幫助。
這是我的代碼:
<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代碼
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查詢
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
假設 StatusID 是 mstUser 的主鍵 如果您只需要檢查記錄是否存在,那么...
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
編輯
根據@marc_s 使用 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.