Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

ss

I have a stored procedure that checks if the Email (& Mobile) entered already exists in the database. It returns True if it does (exist), False otherwise. If False is returned (i.e. email & mobile are unique), another stored procedure will insert the user details into the database and register him. All this is done on a single button click.

NEED HELP WITH THIS CODE:

EDIT 2:

 protected void btnRegister_Click(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
    con.Open();



    SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);
    Cmd.CommandType = CommandType.StoredProcedure;
    Cmd.CommandText = "Registration";
    Cmd.Parameters.AddWithValue("@Name", txtName.Text);
    Cmd.Parameters.AddWithValue("@Email", txtEmailAddress.Text);
    Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
    Cmd.Parameters.AddWithValue("@CountryCode", ddlCountryCode.Text);
    Cmd.Parameters.AddWithValue("@Mobile", txtMobileNumber.Text);
    //Cmd.Parameters.Add("@Result", DbType.Boolean);
    SqlParameter sqlParam = new SqlParameter("@Result", DbType.Boolean);
    //sqlParam.ParameterName = "@Result";
    //sqlParam.DbType = DbType.Boolean;
    sqlParam.Direction = ParameterDirection.Output;
    Cmd.Parameters.Add(sqlParam);
    Cmd.ExecuteNonQuery();
    con.Close();
    Response.Write(Cmd.Parameters["@Result"].Value);

}

Question: how do I make it work? How do I make it work with minimum resources? Am I duplicating code? I wanna do this in the most efficient/logical/correct way.

EDIT:

ALTER PROCEDURE [dbo].[usp_CheckEmailMobile]
(   @Name VARCHAR(50), 
@Email NVARCHAR(50), 
@Password NVARCHAR(50), 
@CountryCode INT, 
@Mobile VARCHAR(50), 
@Result BIT OUTPUT)
AS 
BEGIN 

  IF EXISTS (SELECT COUNT (*) FROM AUser WHERE  [Email] = @Email AND [Mobile] = @Mobile) 
 Begin 
 Set @Result=0; --Email &/or Mobile does not exist in database
End
 ELSE
 Begin
 --Insert the record & register the user 
INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) VALUES (@Name, @Email, @Password, @CountryCode, @Mobile)  
  Set @Result=1;
 End

END    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
128 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can perform the operation via your usp_CheckEmailMobile stored procedure:

Inside your stored procedure do a check like this:

-- Check that record doesn't exist
IF NOT EXISTS (SELECT TOP 1 FROM AUser WHERE Email=@Email and Mobile=@Mobile)
       -- Insert record
       INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) 
       VALUES  (@Name, @Email, @Password, @CountryCode, @Mobile)  

This will insert the record if it doesn't exist. You will just need to add the other parameters on to this stored procedure.

You can use the True / False value and say 'Registration Successful' or 'User already exists' depending on the value returned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...