asp.net网页中的主键错误 [英] primary key error in asp.net web page

查看:83
本文介绍了asp.net网页中的主键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于网络的应用程序(员工注册表)。我在其中存储SQL服务器中的用户信息。我有文本框插入雇用id号,在textbox textchange事件上(它将验证id是否有效,来自另一个只存储idno.s的表),如果它是有效的id no。然后它将允许输入其他细节,如(Ename,地址,联系号码,......等)。单击提交按钮后,所有这些信息将保存在名为EmpRegister的数据库表中。在EmpRegister表中,我在EID列上有主键,如果员工尝试注册多次,则会出现服务器错误。

任何人都可以帮我解决这个问题,在文本更改事件中有两件事情应该发生(1.它应该检查它是否是一个有效的ID来自数据库的EIDS表,2。如果该ID已经在EmpRegister表中注册,那么它不应该给出服务器错误,而是我们可以给出一条错误消息已经注册)。

请帮助我编写代码,我是编程新手。在此先感谢。

解决方案

在插入检查之前进行验证....员工是否已注册..exampal



 cmd =( 选择计数(*)来自员工,其中emp_name =' + txtEmpName.Text +  ',con); 
Int32 hh = convert.ToInt32(cmd.ExecuteScaler());

if (hh == 0
{
// 您的插入符号
}
else
{
// 提供员工已注册的提醒消息
}



根据您的要求更改选择,即检查EID或empname ...


只需将EID定义为主键字段,在插入数据时,它将检查表中的唯一而非空数据。

如果用户已经存在且具有相同的EID,则会引发错误。


我不确定为什么你需要另一个表来存储员工ID号码。您可以将所有内容都包含在一个表中,然后使用 IDENTITY(Property) [ ^ ]自动生成EId列。以下是表格外观的示例。

  CREATE   TABLE  EmpRegister 

EId INT IDENTITY (< span class =code-digit> 1 , 1 CONSTRAINT PK_EmpRegister PRIMARY KEY - 这是自动生成的主键
EmployeeId VARCHAR 100 ), - 这是用户输入的员工ID。
EName < span class =code-keyword> VARCHAR ( 100 ),
地址 VARCHAR 100 ),...





当用户输入员工ID时,你可以检查它是否存在

SELECT COUNT(*)FROM EmpRegister WHERE EmployeeId =''用户输入的值''

如果用户输入的EmployeeId已存在,则count将返回非零值,否则返回0。


I am working on a webbased application(Employee registration form). In which I am storing user information in SQL server. I have text box to insert the employ id no., on textbox textchange event (it will verify the id is valid or not,from another table where only employ idno.s are stored), If it is a valid id no. then it will allow to enter other details like(Ename, address, contact no:,...etc). On clicking submit button all this information will be saved in database table called EmpRegister. In EmpRegister table I have primary key on EID column, if an employee try to register for more than one time, it is giving server error.
Can anybody help me regarding this, On text change event two things should happen (1. It should check it is a valid id or not fro EIDS table of database, 2. If that id is already registered in EmpRegister table,then it should not give server error, Instead we can give an error message that "Already Registered").
Kindly help me with the code, I am new to programming. Thanks in advance.

解决方案

do your validations...before inserting check ....is employee is registered or not..exampal

cmd=("Select count(*) from employee where emp_name='"+txtEmpName.Text+"'",con);
Int32 hh=convert.ToInt32(cmd.ExecuteScaler());

if(hh==0)
{
//your insert coading
}
else
{
//give alert message employee already registered
}


change select as per your requirement i.e in where check EID or empname...


Just define EID as a primary key field and while inserting a data it will check an unique and not null data from table.
and if user is already existing with a same EID it will throw an error.


I am not sure why you would need another table to store Employee Id numbers. You can include everything in one table and you can use IDENTITY (Property)[^] to auto generate the EId column. Here is a sample of how your table would look.

CREATE TABLE EmpRegister
(
   EId INT IDENTITY(1,1) CONSTRAINT PK_EmpRegister PRIMARY KEY, --This is the primary key that is auto generated
   EmployeeId VARCHAR(100), --This is the employee ID entered by user.
   EName VARCHAR(100),
   Address VARCHAR(100),...
)



When User enters the Employee Id you can check if it exists or not like this
SELECT COUNT(*) FROM EmpRegister WHERE EmployeeId = ''Value entered by user''
If the EmployeeId entered by user already exists the count will return a non zero value else it will return 0.


这篇关于asp.net网页中的主键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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