我插入记录到数据库中,我检查了重复的电子邮件ID,但事情是电子邮件已经存在,则显示的信息并记录也越来越插入DB检查是否存在重复的电子邮件
我BAL
public int insert(string firstname,string lastname,DateTime dob,string gender,string mobile,string country,string state,string email,string password)
{
ProfileMasterDAL dal=new ProfileMasterDAL();
try
{
return ProfileMasterDAL.Insert(firstname, lastname, dob, gender, mobile, country, state, email, password);
}
catch (Exception ex)
{
throw ex;
}
我的UI
try
{
chk = pmBLL.insert(firstname, lastname, dob, gender, mobile, country, state, email, password);
if (chk >= 0)
{
Response.Write("<script language='javascript'>alert('Email already exists');</script>");
}
else
{
result = pmBLL.insert(firstname, lastname, dob, gender, mobile, country, state, email, password);
if (result > 0)
{
Session["username"] = TextBox1.Text;
我插入存储过程
ALTER procedure [dbo].[Insertreg]
(@id int output,@FirstName varchar (50),@LastName varchar(50) ,@Dob datetime,
@Gender varchar(20) ,@MobileNo nchar(10) ,@Country varchar(50) ,
@State varchar (50),@EmailId varchar (50),@Password nchar (15),@result int output
)
as
begin
if exists(select EmailId from Profile_Master where [email protected])
set @result=0
else
begin
set @result=1
insert into Profile_Master(FirstName,LastName,Dob,Gender,MobileNo,Country,State,EmailId,Password)
values
(@FirstName,@LastName,@Dob,@Gender,@MobileNo,@Country,@State,@EmailId,@Password)
set @id=SCOPE_IDENTITY()
return
end
end
的存储过程现在看起来更好。如果电子邮件已存在,则存储的proc应该返回0,否则应该插入一条新记录并返回1.您能否更好地解释错误,即您期望发生什么?以及实际发生了什么? – Les 2012-08-13 12:02:13
如果您收到电子邮件已存在的错误,可能它已存在。您的原始帖子显示的代码会尝试插入它,即使它已经存在。开始和结束将防止此SP尝试插入副本。 – Les 2012-08-13 12:03:50
此外,您的代码调用pmBLL.Insert,但您的SP是Insertreg。您不显示任何代码将插入映射到SP。 – Les 2012-08-13 12:06:32