I have a simple C# windows form which acts as a login, but also has a form to change the password of a user. When you click on Change Password the form loads with a text box of current password, new pass and confirm new pass, and one save button. and i have store username in label so that current password can be checked if it is valid from database or not..

I am storing these in a SQL Database which I created in Microsoft sql server 2008...

The code is as follows so far.

SqlConnection connect = new SqlConnection(str);
connect.Open(); 
string username = label_username.Text; 
string password = textBox_Current.Text; 
string newPassword = textBox_New.Text; 
string confirmPassword = textBox_Verify.Text;
 string sqlquery = "UPDATE [Member] SET Password=@newpass where Username=@username"; SqlCommand cmd = new SqlCommand(sqlquery, connect); cmd.Parameters.AddWithValue("@newpass", textBox_Verify.Text); cmd.Parameters.AddWithValue("@username", label_username.Text); cmd.Parameters.AddWithValue("@password", textBox_Current.Text); 
cmd.Connection = connect;
cmd.ExecuteNonQuery();
sqlDataReader reader = null; 
reader = cmd.ExecuteReader();
 while (reader.Read()) 
{
 if ((textBox_New.Text == reader["newPassword"].ToString()) & (textBox_Verify.Text == (reader["confirmPassword"].ToString()))) { } 
}
 MessageBox.Show("Password Changed Successfully!");
 this.Close();

............................................................ while executing above code, password change but i want to check validation like if users type wrong password in current password..newpassword and confirm password .. aswell as when user click on first save bottom blank password should not store in database...rather should give message please type the password...

Edited 5 Years Ago by __avd: Added [code] tags.

on button Click you can fetch the original password value from the database store it in a variable and once you get the value run a if else loop where u can check the current password with the value fetched from database and if the condition succeeds then execute the update code for it .
and to check for blank text box u can use a requiredfield validator or u can check like this way

if(textbox1.text == "")
{
//execute error code
}
else
{
// execute following code
}

Edited 5 Years Ago by DeadSoul: n/a

For that you have to check old password which is entered at time of changing is matched with saved password or not.If it does not match then show error like do not match your your old password.If match then change it.

Change sql sa password by using command prompt
Open a command prompt (Start -> Run -> cmd)
Type the follow commands, and press Enter after each line.
Osql –S yourservername –E
1>EXEC sp_password NULL, ’yourpassword’, ’sa’
2>GO
The yourservername is the name of your server, yourpassword is the new sql sa password.

Shiva check the requirement of the question he has not asked how to chnage the server access password he needs to change the password which is in his table

Edited 5 Years Ago by DeadSoul: Spelling Correction

Hi,
To check something has been entered in the new and confirm textboxes use a required field validator on both and a compare validator on one (to make sure the text entered in it matches the text entered into the other). These validators will validate when the button is clicked and so you don't need to handle them in your code when updating. If there is a problem the code to change the password simply won't get called. The only thing left to do is get the current password from the database, check it against the current password the user entered and if they match, proceed to storing the new value.

Hope that helps,

This article has been dead for over six months. Start a new discussion instead.