在按钮单击事件时检查数据库中的重复值
本文关键字:数据库 检查 按钮 单击 事件 | 更新日期: 2023-09-27 18:05:10
我有一个要求,我想检查用户是否没有一次又一次地插入相同的Project
, Survey No
。
如果两次输入相同的组合,点击按钮
这是我的HTML:-
<td class="label">
Project :
</td>
<td class="field" style="width: 10%;">
<asp:DropDownList ID="ddlProject" runat="server" Width="80%" AutoPostBack="true"
OnSelectedIndexChanged="ddlProject_SelectedIndexChanged">
<asp:ListItem Value="0">--Select--</asp:ListItem>
</asp:DropDownList>
</td>
<td class="label">
Survey No :
</td>
<td class="field">
<asp:TextBox ID="txtSurvey1" runat="server" Width="80%" ReadOnly="true"></asp:TextBox>
</td>
我尝试了下面的链接,但它不是为组合。这只是一个文本框的值,所以它不是在我的情况下工作。
使用javascript检查重复数据
请告诉我如何处理组合部分
我对你链接的答案做了一些修改
var projval=$('#ddlProject').val();
var survey=$('#txtSurvey1').val();
$.ajax({
type: "POST",
url: "YourPage.aspx/DoesDataExist",
data: JSON.stringify({
proj :projval, // the names of the properties must match with the parameters in the backend function
surv :survey
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if(msg.d) {
// This is a duplicate, alert user with message
// Block the server-side click from happening with return false;
return false;
}
}
});
这是后端webservice
[WebMethod]
public static bool DoesDataExist(string proj, string surv )
{
SqlCommand commandrepeat1 = new SqlCommand("Select * from table where project="+proj+" and Survey="+surv+" ");
commandrepeat1.Connection = objconnection;
objconnection.Close();
objconnection.Open();
SqlDataReader drmax1;
drmax1 = commandrepeat1.ExecuteReader();
drmax1.Read();
if (drmax1.HasRows)
{
objconnection.Close();
return true;
}
objconnection.Close();
return false;
}