.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Rememeber Me option in ASP.NET With C#

Posted By:Athira Sreejith       Posted Date: February 24, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Try like this to preform Remember Me Option in Asp.Net using c#.

  protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            LearnHelper objLogin = new LearnHelper();
            bool IsAvailable = false;
            HttpCookie myCookie = new HttpCookie("myCookie");
            bool IsRemember = chkRememberme.Checked;
            IsAvailable = objLogin.CheckUserLogin(txtUsername.Text.Trim(), txtPassword.Text.Trim());
            if (IsAvailable)
            {
                DataSet dsData = objLogin.CheckUserAvailablity(txtUsername.Text.Trim(), txtPassword.Text.Trim());
                if (dsData.Tables[0].Rows.Count > 0)
                {
                    Session["Username"] = txtUsername.Text.Trim();
                    Session["UserId"] = dsData.Tables[0].Rows[0].ItemArray[0].ToString();
                }
                if (IsRemember)
                {
                    myCookie.Values.Add("username", txtUsername.Text);
                    myCookie.Values.Add("password", txtPassword.Text);
                    myCookie.Expires = DateTime.Now.AddDays(15);
                    Response.Cookies.Add(myCookie);
                    Response.Redirect("Dashboard.aspx");
                }
                else
                {
                    myCookie.Values.Add("username", string.Empty);
                    myCookie.Values.Add("password", string.Empty);
                    myCookie.Expires = DateTime.Now.AddMinutes(5);
                    Response.Cookies.Add(myCookie);
                }
            }
            else
            {
                lblMessage.Text = "Invalid login details or else your account has been blocked!";
            }
        }

-------------------
LearnHelper.cs:
===============================

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Learn
{
    public class LearnHelper
    {
        string strSuccess = string.Empty;

        public DataSet CheckUserAvailablity(string strUname, string strPwd)
        {
            SQLHelper sqlHelper = new SQLHelper();
            SqlParameter sp1 = new SqlParameter("@UserName", strUname);
            SqlParameter sp2 = new SqlParameter("@Password", strPwd);
            DataSet dsData = sqlHelper.ExecuteDataSetSPDB("CheckUserloginAvailablity", new SqlParameter[] { sp1, sp2 });
            return dsData;
        }

        public bool CheckUserNameAvailablity(string strUsername)
        {
            SQLHelper objHelp = new SQLHelper();
            bool IsSuccess = false;
            SqlParameter p1 = new SqlParameter("@UserName", strUsername);
            DataSet dsUser = objHelp.ExecuteDataSetSPDB("CheckUserNameAvailablity", new SqlParameter[] { p1 });
            if (dsUser.Tables[0].Rows.Count > 0)
            {
                IsSuccess = true;
            }
            return IsSuccess;
        }

        public bool CheckUserLogin(string strUsername, string strPassword)
        {
            SQLHelper objCheck = new SQLHelper();
            bool IsAvailable = false;
            SqlParameter sp1 = new SqlParameter("@UserName", strUsername);
            SqlParameter sp2 = new SqlParameter("@Password", strPassword);
            DataSet ds = objCheck.ExecuteDataSetSPDB("CheckUserLogin", new SqlParameter[] { sp1, sp2 });
            if (ds.Tables[0].Rows.Count > 0)
            {
                IsAvailable = true;
            }
            return IsAvailable;
        }

        public string CreateNewUser(string UserName, string Password, string RePassword, string FirstName, string LastName, string Email)
        {
            SQLHelper objAdd = new SQLHelper();
            bool IsSuccess = false;
            SqlParameter sp1 = new SqlParameter("@UserName", UserName);
            SqlParameter sp2 = new SqlParameter("@Password", Password);
            SqlParameter sp3 = new SqlParameter("@RePassword", RePassword);
            SqlParameter sp4 = new SqlParameter("@FirstName", FirstName);
            SqlParameter sp5 = new SqlParameter("@LastName", LastName);
            SqlParameter sp6 = new SqlParameter("@Email", Email);
            IsSuccess = objAdd.ExecuteNonQuerySPDB("UpdateRegisteration", new SqlParameter[] { sp1, sp2, sp3, sp4, sp5, sp6 });
            if (IsSuccess)
            {
                strSuccess = "Inserted Successfylly";
            }
            else
            {
                strSuccess = "Insertion Failed";
            }
            return strSuccess;
        }
    }
}
==================================

SQLHelper.cs:
*********************

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public class SQLHelper
{
    private string strConnectionString = string.Empty;

    public SQLHelper()
    {
        // strConnectionString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
    }

    #region Common Function definition to execute any query

    ///


    /// Create the connection with the DataBase using web.config Appsettings tag.
    ///

    /// sqlconnection object
    public SqlConnection connection()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString);
        return con;
    }

    ///


    /// Used to execute query using ExecuteNonQuery() Method
    ///

    ///
    /// Boolean
    public bool ExecuteNonQueryDB(string Query)
    {
        if (Query != "")
        {
            SqlConnection m_Scon = connection();
            SqlCommand m_Scmd = new SqlCommand(Query, m_Scon);
            m_Scon.Close();
            m_Scon.Open();
            int m_Sdr = m_Scmd.ExecuteNonQuery();
            if (m_Sdr > 0)
            {
                if (m_Scon.State == ConnectionState.Open)
                {
                    m_Scon.Close();
                }
                return true;
            }
            else
            {
                if (m_Scon.State == ConnectionState.Open)
                {
                    m_Scon.Close();
                }
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    ///


    /// Used to execute query using ExecuteReader() Method
    ///

    ///
    /// IDataReader
    public SqlDataReader ExecuteReaderDB(string Query)
    {
        SqlConnection m_Scon = connection();
        SqlCommand m_Scmd = new SqlCommand(Query, m_Scon);
        m_Scon.Close();
        m_Scon.Open();
        SqlDataReader m_Sdr = m_Scmd.ExecuteReader(CommandBehavior.CloseConnection);
        return m_Sdr;
    }

    ///


    /// Used to execute query using ExecuteDataSet() Method
    ///

    ///
    /// DataSet
    public DataSet ExecuteDataSetDB(string Query)
    {
        if (Query != "")
        {
            SqlConnection m_Scon = connection();
            SqlCommand m_Scmd = new SqlCommand(Query, m_Scon);
            DataSet m_DSObj = new DataSet();

            SqlDataAdapter adr = new SqlDataAdapter(m_Scmd);
            adr.Fill(m_DSObj);

            if (m_Scon.State == ConnectionState.Open)
            {
                m_Scon.Close();
            }

            return m_DSObj;
        }
        else
        {
            return null;
        }
    }

    ///


    ///Used to execute the stored procedure and return DataSet
    ///

    ///
    ///
    ///DataSet
    public DataSet ExecuteDataSetSPDB(string ProcedureName, SqlParameter[] Parameters)
    {
        if (ProcedureName != "")
        {
            SqlConnection m_Scon = connection();
            SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
            m_Scmd.CommandType = CommandType.StoredProcedure;
            DataSet m_DSObj = new DataSet();
            if (Parameters != null)
            {
                foreach (SqlParameter para in Parameters)
                {
                    m_Scmd.Parameters.Add(para);
                }
            }
            SqlDataAdapter adr = new SqlDataAdapter(m_Scmd);
            adr.Fill(m_DSObj);

            if (m_Scon.State == ConnectionState.Open)
            {
                m_Scon.Close();
            }

            return m_DSObj;
        }
        else
        {
            return null;
        }
    }

    ///


    /// Used to execute the stored procedure and return IDataReader
    ///

    ///
    ///
    /// IDataReader
    public SqlDataReader ExecuteReaderSPDB(string ProcedureName, SqlParameter[] Parameters)
    {
        if (ProcedureName != "")
        {
            SqlConnection m_Scon = connection();
            SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
            m_Scmd.CommandType = CommandType.StoredProcedure;
            m_Scon.Close();
            m_Scon.Open();
            if (Parameters != null)
            {
                foreach (SqlParameter para in Parameters)
                {
                    m_Scmd.Parameters.Add(para);
                }
            }
            SqlDataReader m_Sdr = m_Scmd.ExecuteReader(CommandBehavior.CloseConnection);


            return m_Sdr;
        }
        else
        {
            return null;
        }
    }

    ///


    /// Used to execute the stored procedure using executenonquery
    ///

    ///
    ///
    /// Boolean
    public bool ExecuteNonQuerySPDB(string ProcedureName, SqlParameter[] Parameters)
    {
        if (ProcedureName != "")
        {
            SqlConnection m_Scon = connection();
            SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
            m_Scmd.CommandType = CommandType.StoredProcedure;
            m_Scon.Close();
            m_Scon.Open();
            if (Parameters != null)
            {
                foreach (SqlParameter para in Parameters)
                {
                    m_Scmd.Parameters.Add(para);
                }
            }
            int rowsAffected = m_Scmd.ExecuteNonQuery();

            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    ///


    /// Used to execute stored procedure using executescalar method
    ///

    ///
    ///
    /// string
    public string ExecuteScalarSPDB(string ProcedureName, SqlParameter[] Parameters)
    {
        if (ProcedureName != "")
        {
            SqlConnection m_Scon = connection();
            SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
            m_Scon.Close();
            m_Scon.Open();
            if (Parameters != null)
            {
                foreach (SqlParameter para in Parameters)
                {
                    m_Scmd.Parameters.Add(para);
                }
            }
            object rowsID = m_Scmd.ExecuteScalar();

            return rowsID.ToString();
        }
        else
        {
            return "";
        }
    }

    #endregion
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend