Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
hi,
 
Anyone know this?
 

C# code for change password?
 
i have a settings page...in that page i should do the password changes..
code:
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 12 Feb '12 - 18:32
Edited 12 Feb '12 - 18:42

Comments
CRDave1988 - 13 Feb '12 - 0:39
very unclear question. improve this.
Varun Sareen - 13 Feb '12 - 0:40
What password? have you tried something? Please share some code that may put light on where have you stuck.
AshishChaudha - 7 Jul '12 - 1:34
and ??? where the exception is???

4 solutions

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:
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
  Permalink  
Comments
sathiyak - 13 Feb '12 - 4:13
thanks...
aniketyadav7 - 13 Feb '12 - 4:18
If it has helped you then accept the answer and Vote it.
aniketyadav7 - 13 Feb '12 - 4:23
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
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");
 
}
 
  Permalink  
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();
  Permalink  
Comments
yshu gurung - 8 Oct '12 - 0:00
hello sir ... me getting again and again same error ("Old Password is not valid.", "Update Failed") help ! help!
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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 213
1 Sergey Alexandrovich Kryukov 159
2 Richard MacCutchan 150
3 Maciej Los 136
4 Tadit Dash 110
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,917
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,125


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid