How to store and retrieve multiple values in a single Session in ASP.Net

Last Reply 6 months ago By dharmendr

Posted 6 months ago

plz help me with the code and solution

<asp:TextBox ID="txtUname" runat="server" placeholder="UserName" class="textStyle"></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server" placeholder="Password" class="textStyle"></asp:TextBox>
<asp:Button ID="btnLogin" runat="server" Text="Login" onclick="ValidateUser" />    
<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label> 

Code

protected void ValidateUser(object sender, EventArgs e)
{
    int userId = 0;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("Validate_User"))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Username", txtUname.Text.Trim());
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
            cmd.Connection = con;
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            userId = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
            if (dt.Rows.Count > 0)
            {
                Session["uid"] = dt.Columns[0].ToString();
                Session["Username"] = dt.Columns[1].ToString();
                Session["city"] = dt.Columns[2].ToString();
                Response.Redirect("Redirectform.aspx");
                Session.RemoveAll();
            }
            else
            {
                lblmsg.Text = "You're username and word is incorrect";
                lblmsg.ForeColor = System.Drawing.Color.Red;

            }
        }
        switch (userId)
        {
            case -1:
                lblmsg.Text = "Username and/or password is incorrect.";
                break;
            case -2:
                lblmsg.Text = "Account has not been activated.";
                break;
            default:
                //FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
                break;
        }
    }
}
This is my stored Procedure..validated user

ALTER  PROCEDURE [dbo].[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

 

You are viewing reply posted by: dharmendr 6 months ago.
Posted 6 months ago

You can't ask multiple queries within a question. This is against forum rules and necessary to maintain clean forum.
It is requested Mark Answer the replies when question is answered and ask a new question as a responsible member to help fellow programmers around the world.
Note: If you don't mark answers, people will loose interest in your questions.

I agree, here is the link: http://e-iceblue.com/free-api