Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
hi,

Anyone know this?


C# code for change password?

i have a settings page...in that page i should do the password changes..
code:
C#
protected void btnchange_Click(object sender, EventArgs e)
  {
      int changed;
      string loginemailid = txtloginemailid.Text.Trim();
      string oldpass = txtoldpassword.Text.Trim();
      string newpass = txtnewpassword.Text.Trim();


      if (loginemailid != null && oldpass != null)
      {
          changed = mydac.changeuserpassword(loginemailid, oldpass, newpass);
          if (changed == 1)
          {
              lblmsg.Text = "Your new Password Updated";

          }
      }
      else
      {
          lblmsg.Text = "Updation Failed,Ensure Your Login Information Is Correct";
      }
  }
Posted
Updated 8-Jun-17 18:59pm
v3
Comments
CRDave1988 13-Feb-12 0:39am    
very unclear question. improve this.
Varun Sareen 13-Feb-12 0:40am    
What password? have you tried something? Please share some code that may put light on where have you stuck.
AshishChaudha 7-Jul-12 1:34am    
and ??? where the exception is???

If your conditions are depending about each other, an if, else if, else could make it work.

1. Firstly put your code within try catch statement so that you will come to know exact error. Nothing is wrong in this statement

if (Userdetails.password != textBox_Current.Text)
2. Tell exactly how you are executing your program .Where have you declared this class and what is the scope of class.
 
Share this answer
 
class Userdetails
	{
            public static string username;
            public static string password;
     }

SqlConnection Connection = new SqlConnection("Data Source=FARZADFWADIA-PC;Initial Catalog=HRMS;Integrated Security=True");
Connection.Open();

if (textBox_New.Text.Length < 4)
	{
		MessageBox.Show("The Length of the password must be 			atleast in four chars.", "Update Failed"); return;
	}

if (textBox_New.Text != textBox_Verify.Text)
	{
		MessageBox.Show("New Password dose not match Confirm 			Password.", "Update Failed"); return;
	}

if (Userdetails.password != textBox_Current.Text)
	{
		MessageBox.Show("Old Password is not valid.", "Update 		Failed");
		return;
	}

Userdetails.password = textBox_New.Text;
SqlCommand cmd = new SqlCommand("update Login set password=@UserPassword where username="+Userdetails.username);
cmd.Parameters.AddWithValue("@UserPassword", textBox_New.Text);

if (cmd.ExecuteNonQuery().ToString() == "0")
	{
		MessageBox.Show("Password changed successfully.", 			"Process Completed", MessageBoxButtons.OK, 				MessageBoxIcon.Information);
	}
else
	{
		MessageBox.Show("Due to some problems your password 			cannot be changed. Contact your administrator to 			change it 	or try again later.", "Update Failed", 			MessageBoxButtons.OK,MessageBoxIcon.Warning);
	}

this.Close();
 
Share this answer
 
Comments
yshu gurung 8-Oct-12 0:00am    
hello sir ... me getting again and again same error ("Old Password is not valid.", "Update Failed")
help !
help!
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.

Sample Code:
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;
       }
    }


Hope through this sample code you will get an idea
 
Share this answer
 
Comments
sathiyak 13-Feb-12 4:13am    
thanks...
Aniket Yadav 13-Feb-12 4:18am    
If it has helped you then accept the answer and Vote it.
Aniket Yadav 13-Feb-12 4:23am    
Thanks for Downvoting
You can use it by various methods
1 =>Change Password Control
2=>You can take three text boxes.
input:Old password
Input:New
Input:Confirm(Match both new and confirm textbox text)
3=>
change-user-password-in-asp-net-forms-authentication

Simple way

as
C#
protected void Button1_Click(object sender, EventArgs e)
{

string paswd;
paswd="NewPass" // set your password
String str = "update reg set password=@pass where u_name=@u_name and password=@oldpass ";
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("oldpass",oldpass.Text);
cmd.Parameters.AddWithValue("pass",paswd);
cmd.Parameters.AddWithValue("u_name",txt_nPW.Text);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Password Changed");

}
 
Share this answer
 

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