Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to validate user and return multiple column values on login using SP
I wan't to make changes in below procedure and add this line of code or concept ,
as the previous procedure does't not return Uid, City and email..so plz guide me changes

Select UserId, Username, City, email from Users WHERE Username = @Username AND [Password] = @Password

and the remaining code does't get affected...



......................

What I have tried:

SQL
create PROCEDURE [Validate_User]
@Username NVARCHAR(20),
@Password NVARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @UserId INT, @LastLoginDate DATETIME

SELECT @UserId = UserId, @LastLoginDate = LastLoginDate
FROM Users WHERE Username = @Username AND [Password] = @Password

IF @UserId IS NOT NULL
BEGIN
IF NOT EXISTS(SELECT UserId FROM UserActivation WHERE UserId = @UserId)
BEGIN
UPDATE Users
SET LastLoginDate = GETDATE()
WHERE UserId = @UserId
SELECT @UserId [UserId] -- User Valid
END
ELSE
BEGIN
SELECT -2 -- User not activated.
END
END
ELSE
BEGIN

SELECT -1 -- User invalid.
END
END
Posted
Updated 6-Oct-16 22:24pm
Comments
Sinisa Hajnal 7-Oct-16 4:22am    
It seems fine, what is the problem here? Besides the fact that you send (and keep) the password in parameter / table :)
safal.786 7-Oct-16 7:21am    
this code is correct ..but i need to retrieve userid, city and email also from the same User table , so what changes, i should do..

1 solution

Hello
I have answered same question at :

CodeProjects

C#
protected void Page_Load(object sender, EventArgs e)
        {
            ProductList();
 
            MasterDCDataContext db = new MasterDCDataContext();
            var q = db.MOptions;
            lvAboutus.DataSource = q;
            lvAboutus.DataBind();
 
            lvAddress.DataSource = q;
            lvAddress.DataBind();
 
            var p = db.Posts;
            lvFooterNews.DataSource = p.OrderByDescending(c => c.PoID).Take(8);
            lvFooterNews.DataBind();
 
            var t = Session["LoginID"];
            mvLogin.ActiveViewIndex = Convert.ToInt32(t);
            int role = Convert.ToInt32(t);
 
            var m = Session["UserID"];
            int Us = Convert.ToInt32(m);
 
            if (Us >= 1)
            {
                string strConnection = "Data Source =Ali-HP;Database=EzBuy;Integrated Security=yes";
                using (SqlConnection Connection = new SqlConnection(strConnection))
                {
                    String strQuery = "SELECT * FROM [User] WHERE UsID = " + Us + " ";
                    SqlCommand Command = new SqlCommand(strQuery,Connection);
                    Command.Connection.Open();
                    SqlDataReader rdr = Command.ExecuteReader();
                    rdr.Read();
                    switch(role)
                    {
                        case 1: lblUserName.Text = rdr[0].ToString() + " " + rdr[1].ToString();
                            break;
                        case 2: lblAdminName.Text = rdr[0].ToString() + " " + rdr[1].ToString();
                            break;
                    }
                    Command.Connection.Close();
                }
            }
 
        }
 
        protected void ValidateUser(object sender, EventArgs e)
        {
            int userId = 0;
            string roleId = "";
            string roles = string.Empty;
            string strConnection = "Data Source =Ali-HP;Database=EzBuy;Integrated Security=yes";
            using (SqlConnection Connection = new SqlConnection(strConnection))
            {
                using (SqlCommand Command = new SqlCommand("ValidateLogin"))
                {
                    Command.CommandType = CommandType.StoredProcedure;
                    Command.Parameters.AddWithValue("@Username", loginBox.UserName);
                    Command.Parameters.AddWithValue("@Password", loginBox.Password);
                    Command.Connection = Connection;
                    Command.Connection.Open();
                    SqlDataReader rdr = Command.ExecuteReader();
                    rdr.Read();
                    userId = Convert.ToInt32(rdr["UserId"].ToString());
                    roleId = rdr["Roles"].ToString();
                    Command.Connection.Close();
                }
                switch (userId)
                {
                    case -1:
                        loginBox.FailureText = "Invalid UserName/Password";
                        break;
                    case -2:
                        loginBox.FailureText = "InActive Account";
                        break;
                    default:
                        switch(roleId)
                        {
                            case "User":
                                Session["LoginID"] = 1;
                                Session["UserID"] = userId;
                                Response.Redirect("UHome.aspx");
                                break;
                            case "Admin":
                                Session["LoginID"] = 2;
                                Session["UserID"] = userId;
                                Response.Redirect("UHome.aspx");
                                break;
                        }
                        break;
                }
            }
        }


and the procedure is :

SQL
USE [EzBuy]
GO
/****** Object:  StoredProcedure [dbo].[ValidateLogin]    Script Date: 2016-10-06 13:17:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ValidateLogin]
      @Username NVARCHAR(50),
      @Password NVARCHAR(50)
AS
BEGIN
      SET NOCOUNT ON;
      DECLARE @UserID INT, @LastLoginDate DATETIME, @RoleId INT
      
      SELECT @UserID = UserID, @LastLoginDate = UsLastLogin,  @RoleId = RoleId
      FROM Login WHERE UserName = @Username AND UPassword = @Password
      
      IF @UserId IS NOT NULL
      BEGIN
            IF  EXISTS(SELECT UserID FROM Login WHERE UserID = @UserID)
            BEGIN
                  UPDATE Login
                  SET UsLastLogin = GETDATE()
                  WHERE UserID = @UserID
                  
                  SELECT @UserID as UserID,
                              (SELECT RoleName FROM RoleChart
                               WHERE RoleId = @RoleId) [Roles]
            END
            ELSE
            BEGIN
                  SELECT -2 as UserID, '' [Roles]-- User not activated.
            END
      END
      ELSE
      BEGIN
            SELECT -1 as UserID, '' [Roles] -- User invalid.
      END
END



hope it works
 
Share this answer
 
v2
Comments
safal.786 7-Oct-16 7:16am    
at least post the link or url of the code you have answered previously....so that i can check on it.
Ali Majed HA 7-Oct-16 6:26am    
Can You please speak English? I did NOT get what you mean.
Ali Majed HA 7-Oct-16 7:25am    
I have done it !!! Just Click on "CodeProject" in the solution. it is the Link
Ali Majed HA 7-Oct-16 7:40am    
would you please vote the answer if you think it is useful?

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