检查重复的电子邮件

时间:2012-08-13 09:53:37

标签: c# asp.net

我正在将记录插入数据库中,我正在检查重复的电子邮件ID,但事情是电子邮件已存在消息显示并且记录也会插入到数据库中

我的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;
      }

我的用户界面

 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 EmailId=@EmailId)
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

4 个答案:

答案 0 :(得分:1)

使用开头和结尾作为你的其他声明

if exists(select @EmailId from Profile_Master where EmailId=@EmailId) 
    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() 
end

答案 1 :(得分:1)

建议

1)在电子邮件ID上创建唯一密钥

2)在SP下面使用

 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

   declare @iCount int
    select @iCount = count(EmailId) from Profile_Master where EmailId=@EmailId

  SET @result = @iCount

  if @result > 0
 BEGIN
    set @result=0
    return
     END
    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 @id
 end 
    end

答案 2 :(得分:0)

您需要在BAL层检查此项,而不是UI层。

答案 3 :(得分:0)

你的SQL查询

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()

是与条件分开的任何条件使用开始和结束以正确的方式你将得到解决方案

Begin
if exists(select @EmailId from Profile_Master where EmailId=@EmailId)
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()
End
End