Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
how to change password in asp.net and using c#
Posted

C#
private Boolean changePassword(string newpassword, string oldpassword, string username)//if change success return true otherwise not
    {
        DataSet ds = new DataSet();
        Boolean result = false;

        if (newpassword.Equals(oldpassword))
        {
            string sqlquery = "select password from tbl_users where username='" + username + "'";
            
            SqlConnection con = new SqlConnection("CONNECTIONSTRING");
            con.Open();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
                da.Fill(ds);
                if (!ds.Tables[0].Rows[0]["password"].Equals(newpassword))
                {
                    SqlCommand cmd = new SqlCommand("update tbl_users set password='" + newpassword + "' where username='" + username + "'", con);
                    result = Convert.ToInt32(cmd.ExecuteNonQuery()) == 0 ? true : false;
                }

            }            
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                con.Close();
            }            
        }
        return result;
    }


Hope It will help
 
Share this answer
 
Suppose you have 2 textbox named txtNewPassword and txtConfirmedPassword.
After feeding these two textboxes, On the Save Button Click Event Do The Following:
1. Fetch The Record of current user in the datatable.
2. If recordcount > 0 then fire an update query to update the current user's new password.
C#
if (txtNewPassword.Text == txtConfirmPassword.Text)
    {
        mSQL = "SELECT * FROM TableName WHERE user_name = '" + txtUserName.Text + "' AND password = '" + txtPassword.Text + "'";
        mDT_Save = mDBHelper.GetTable(mSQL);
        if (mDT_Save.Rows.Count > 0)
        {
            for (int i = 0; i < mDT_Save.Rows.Count; i++)
            {
                mSQL = "UPDATE TableName SET password = '" + txtConfirmPassword.Text + "' WHERE user_name = '" + Convert.ToString(mDT_Save.Rows[i]["user_name"]) + "'";
                mDBHelper.ExecuteSQLNonQuery(mSQL);
            }
            MessageBox.Show("Password Changed Successfully");
            txtPassword.Text = txtConfirmPassword.Text;
            return;
       }
       else
       {
            MessageBox.Show("User Not Found");
            return;
       }
    }
 
Share this answer
 
Comments
bbirajdar 20-Jul-12 5:44am    
I guess the discussion is going to get longer if the OP does not understand it
HTML side

XML
<asp:label id="lbUsername" runat="server">Select UserName:</asp:label>
<asp:textbox id="Username" runat="Server"></asp:textbox>
<asp:label id="lbNewPassword" runat="server">New Password:</asp:label>
<asp:textbox id="NewPassword" runat="Server"></asp:textbox>
<asp:button id="reset" runat="server" onclick="reset_Click"  />




//code back at the back or script c#
protected void reset_Click(object sender, EventArgs e)
{
MembershipUser u;
string oldPassword;

u = Membership.GetUser(UserName.Text, false);
try
{
//reset the password and assign it to oldPassword variable
oldPassword = u.ResetPassword();
//call the change password to change the auto reset password to your desired password.
try
{

u.ChangePassword(oldPassword, NewPassword.Text);
rsetError.Text = "Password Hass been successfully Change";
}
catch(MembershipPasswordException e1)
{
rsetError.Text = "Error .";
rsetError.Text += e1.Message;
}
}
catch (MembershipPasswordException e2)
{
rsetError.Text = "Error .";
rsetError.Text +=e2.Message;
return;
}


}
I have program it this way so that instead of give the user the auto generated password you can reset the password base on the password as supplied on the textbox called new password.
 
Share this answer
 
v2

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