如何在插入时识别重复的密钥 [英] how to identify a duplicate key at insertion

查看:90
本文介绍了如何在插入时识别重复的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...
我已经插入了一些数据...
我要注册一个新用户...
但是我给了已经插入的相同用户名...
但用户名是主键..因此它显示不允许重复的键....
我如何在文本框更改位置上贴心?
是否可以说请选择另一个用户名,因为已经选择了它" ...

hi...
i have inserted already some datas...
i am going to register a new user...
but i given same user name which is already inserted...
but the user name is the primary key.. so it show doesn''t allows duplicate key....
how can i intimate at the textbox changed position?
is it possible to say that "please choose another username because its already chosen"...

推荐答案

这很容易

在插入表之前使用此查询

从yourusertable中选择count(*),其中userid =''userid''

如果查询返回大于零,则用户已经存在:)
it is easy task

use this query before inserting into table

select count(*) from yourusertable where userid=''userid''

if Query return is greater than zero then user already exists :)


进一步回答,您可以使用 Ajax对TextBoxonchange事件进行验证..
Further to above Answer, You can do validation on onchange Event of your TextBox using Ajax.
<asp:textbox id="txtName" runat="server" onchange="javascript: Test();" />


您可以参考" Asp.Net Ajax "的提示/技巧.
Simplifying-Asp-Net-Core-Ajax

已更新-或使用onblur JavaScript事件.


You can refer my Tip/Trick for "Asp.Net Ajax".
Simplifying-Asp-Net-Core-Ajax

Updated - Or use onblur JavaScript Event.

<asp:textbox id="txtName" runat="server" onblur="javascript: Test();" />


这篇关于如何在插入时识别重复的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆