Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 19:32pm
Edited 12-Feb-12 19:42pm
v3
Comments
CRDave1988 at 13-Feb-12 0:39am
   
very unclear question. improve this.
Varun Sareen at 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 at 7-Jul-12 1:34am
   
and ??? where the exception is???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 13-Feb-12 4:13am
   
thanks...
aniketyadav7 at 13-Feb-12 4:18am
   
If it has helped you then accept the answer and Vote it.
aniketyadav7 at 13-Feb-12 4:23am
   
Thanks for Downvoting
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 at 8-Oct-12 0:00am
   
hello sir ... me getting again and again same error ("Old Password is not valid.", "Update Failed")
help !
help!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
0 OriginalGriff 436
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 170
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100