Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
While Working on User Login Panel, I came across a situation where i have to update and select statement both in sp, but how would i know if these both statements are executed successfully.


SQL
CREATE PROC uspAppUserLogin
@AppUserEmail varchar(50),@AppUserPassword varchar(max),@AppUserID int OUT,@AppUsername Varchar(25) OUT,
@LastLoginDate varchar(20) out
AS
BEGIN
SET NOCOUNT ON; 
SELECT COUNT(*) FROM TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword
SET @AppUserID =(Select UserId  From TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword)
SET @AppUsername =(Select UserName From TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword)
SET @LastLoginDate = (Select ISNULL(LastLoginDate,GetDate()) From TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword)
Update TblAppUser Set LastLoginDate = GetDate() Where Email = @AppUserEmail and UserPassword =  @AppUserPassword
END


C#
try
          {
              db1.sqlcmd = new SqlCommand("uspAppUserLogin");
              using (SqlDataAdapter sda = new SqlDataAdapter())
              {
                  db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                  db1.sqlcmd.Parameters.AddWithValue("@AppUserEmail", TxtBxEmail.Text.Trim());
                  db1.sqlcmd.Parameters.AddWithValue("@AppUserPassword", hashpassword);
                  db1.sqlcmd.Parameters.Add("@AppUserID", SqlDbType.Int);
                  db1.sqlcmd.Parameters.Add("@AppUsername", SqlDbType.VarChar, 10);
                  db1.sqlcmd.Parameters.Add("@LastLoginDate", SqlDbType.VarChar, 20);
                  db1.sqlcmd.Parameters["@AppUserID"].Direction = ParameterDirection.Output;
                  db1.sqlcmd.Parameters["@AppUsername"].Direction = ParameterDirection.Output;
                  db1.sqlcmd.Parameters["@LastLoginDate"].Direction = ParameterDirection.Output;
                  db1.sqlcmd.Connection = db1.sqlcon;
                  db1.sqlcon.Open();
                  db1.sqlcmd.ExecuteNonQuery();
                  Userid = (int)db1.sqlcmd.Parameters["@AppUserID"].Value;
                  Username = (string)db1.sqlcmd.Parameters["@AppUsername"].Value;
                  recentlogin = (string)db1.sqlcmd.Parameters["@LastLoginDate"].Value;
              }
          }
          catch (Exception ex) { Response.Write(ex.Message); }
          finally
          {
              if (usercount == 1)  // comparing users from table 
              {
                  Session["AppUserName"] = Username;
                  Session["AppUserID"] = Userid;
                  Session["recentlogin"] = recentlogin;
                  Response.Redirect("HomeAppUser.aspx");  //for sucsseful login  
              }
              else
              {
                  db1.sqlcon.Close();
                  ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + "Invalid Login Details " + "');", true);
                  Response.Redirect("CandidateLogin.aspx");
              }
          }
Posted

1 solution

you can do as below

SQL
--added new out parameter 
@userValid bit out
AS
BEGIN
SET NOCOUNT ON; 
--check record exist before update
IF EXISTS(SELECT * FROM TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword)
BEGIN 
    --record exist 
    SET @AppUserID =(Select UserId  From TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword)
    SET @AppUsername =(Select UserName From TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword)
    SET @LastLoginDate = (Select ISNULL(LastLoginDate,GetDate()) From TblAppUser Where Email = @AppUserEmail and UserPassword =  @AppUserPassword)
    Update TblAppUser Set LastLoginDate = GetDate() Where Email = @AppUserEmail and UserPassword =  @AppUserPassword
    Set @userValid = 1 
END
ELSE
    -- no record found 
    Set @userValid = 0
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900