[英]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
.我想检查
StaffID
已存在于数据库中,如果存在,则用户无法使用相同的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?任何人都可以帮我做代码来检查数据库中是否已经存在
staffID
?
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 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 Query 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
Assuming StatusID is the primary key of mstUser If all you need is to check the existence of a record then...假设 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
EDIT
编辑
Code as per @marc_s using If Exists.根据@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.