Wednesday, 25 July 2012

PASSWORD CHECKING & CHANGE PASSWORD




Create  ChangePassword.cs:

public partial class member_ChangePassword : System.Web.UI.Page
{
    BAL b = new BAL();
    string curpass,newpass,conpass;
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

            dt=b.selectupdate();

            foreach (DataRow r in dt.Rows)
            {
                txt_Id.Text = dt.Rows[0][0].ToString();
                lbl_Id.Text = dt.Rows[0][1].ToString();
                lbl_Email.Text = dt.Rows[0][2].ToString();
            }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        b._password = txt_CurPwd.Text;
        b._id = Convert.ToInt32(txt_Id.Text.ToString());
        b._newpass = txt_NewPwd.Text;
        newpass = b._newpass;
        b._conpass = txt_ConfPwd.Text;
        conpass = b._conpass;
        DataTable dt = new DataTable();
        dt = b.checkpassword();
        foreach (DataRow r in dt.Rows)
        {
         
            curpass = dt.Rows[0][0].ToString();
         
        }

        if (curpass != txt_CurPwd.Text)
        {
            Response.Write("Please Enter Valid Password");
        }

        else if (newpass == conpass)
        {

            int result = b.updatepassword();

         
            Response.Write("Updated");

        }
        else
            Response.Write("Not Updated");

    }

    protected void btn_Reset_Click(object sender, EventArgs e)
    {
     
        txt_ConfPwd.Text = "";
        txt_CurPwd.Text = "";
        txt_Id.Text = "";
        txt_NewPwd.Text = "";
     }
}


BAL.CS:

public string _password, _newpass, _conpass;
public int _id;
public BAL()
    {

    }

public int id
    {
        get { return _id; }
        set { _id = value; }
     }

public string password
    {
        get { return _password; }
        set { _password = value; }
    }
public string newpassword
    {
        get { return _newpass; }
        set { _newpass = value; }
    }

public string confirmpassword
    {
        get { return _conpass; }
        set { _conpass = value; }
    }


 public DataTable checkpassword()
    {
        SqlParameter[] d = new SqlParameter[3];
        d[0] = new SqlParameter("@UserId", id);
        d[1] = new SqlParameter("@Password", password);
        d[2] = new SqlParameter("@Flag", 5);
        DataTable chk = DAL.Ret_datatable("get_dt1", d);
        return chk;
    }

    public int updatepassword()
    {
        SqlParameter[] e = new SqlParameter[3];
        e[0] = new SqlParameter("@UserId", id);
        e[1] = new SqlParameter("@password", confirmpassword);
        e[2] = new SqlParameter("@Flag", 6);
        int passupd = DAL.executeNonquery_SP("edit_dt",e);
        return passupd;
    }

DAL.cs:

public class DAL
{
    static SqlConnection con;
    static SqlCommand cmd;
    public DAL()
    {

    }

    public static string connection = (ConfigurationManager.ConnectionStrings["classifiedsConnectionString1"].ConnectionString);

    public static int executeNonquery_SP(String CommandText, SqlParameter[] parameters)
    {
        using (SqlConnection con = new SqlConnection(connection))
        {
            try
            {
                 cmd = new SqlCommand(CommandText, con);
                cmd.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter p in parameters)
                {
                    if (p.Value != null)
                    {
                        cmd.Parameters.Add(p);
                    }

                }
                con.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }


STORED PROCEDURE:

Select:
SELECT UserId,USERNAME,EMAIL
FROM TBL_USER WHERE USERID=@USERID

Update:
UPDATE dbo.tbl_User SET PASSWORD=@PASSWORD
WHERE USERID=@USERID

No comments:

Post a Comment