C # simple movie record management system: Development 3 [Password modification]

Source: Internet
Author: User
Tags parse string connectionstrings

Overview

In the previous issue, we implemented data operations on movie records (add, delete, modify, and query). In this issue, we implemented the password modification function for users.

 

Principles

The principle is very simple. The system obtains the user name entered by the user, executes the select statement to obtain the password under the user name from the database, and matches with the old password entered by the user. If they are the same, the new password is allowed, then run the Update statement to save the new password to the database.

 

As the number of system functions increases, we need to create a main interface form to facilitate the use of various functions.

Create a new form name: FrmMain

 

We create a ToolStrip toolbar in the main form for later function form switching.

Then, right-click the ToolStrip and add the Button.

 

Change the display mode of the Button to Text display.

 

After setting the management interface button, similarly, setting the password to change, return to the login interface and exit the function button directly.

ToolStrip button Management Interface name: tsbManager Password Change name: tsbPwdChange re-Login name: tsbLogin exit system name: tsbExit

 

Code in FrmMain. cs on the main interface

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; /*************************************** ******************* Author: soFlash-blog Park http://www.cnblogs.com/longwu ** Description: FrmMain. the main cs form is used to connect various functional forms ********************************* * *******************/namespace movie record management system {public partial class FrmMain: form {public FrmMain () {InitializeComponent ();} private void tsbManager_Click (object sender, EventArgs e) {// The object FrmManager manager = new FrmManager () of a new Manger Form (); // transfer to the FrmManager form manager. show (); // hide the current form this. hide ();} private void tsbPwdChange_Click (object sender, EventArgs e) {// Similarly, FrmPwdChange pwdChange = new FrmPwdChange (); pwdChange. show (); this. hide ();} private void tsbLogin_Click (object sender, EventArgs e) {// Similarly, FrmLogin login = new FrmLogin (); login. show (); this. hide ();} private void tsbExit_Click (object sender, EventArgs e) {// exit the Application directly. exit ();}}}

Similarly, because the jump interface is changed after successful logon, we need to modify the code of the jump interface after logon.

Modify it in the login form class FrmLogin. cs (as follows)

// If the entered password = password in the database if (pwd = txtPwd. text) {// obtain the user ID Uid = txtName after successful login. text; // indicates that the password under this account is correct and MessageBox is successfully logged on to the system. show ("the system is successfully logged on. You are redirected to the home page... "); // FrmManager manager = new FrmManager (); // manager. show (); // this. hide (); FrmMain main = new FrmMain (); main. show (); this. hide ();}

 

Then, the movie record management form is slightly modified to addBack to Main InterfaceButton.

Back to Main Interface name: btnBack

The Code is as follows:

Private void btnBack_Click (object sender, EventArgs e) {// return to the main interface FrmMain main = new FrmMain (); main. Show (); this. Close ();}

 

Add another directExit SystemButton, name: btnExit.

 

The Code is as follows:

// Directly Exit the system private void btnExit_Click (object sender, EventArgs e) {// directly Exit the system Application. Exit ();}

 

Then we began to design the password modification form (for example)

Form name: FrmPwdChange

Text Box Username: txtUsername old password name: txtOldPwd new password name: txtNewPwd enter new password name: txtNewPwdConfirm

Button to modify name: btnOK return name: btnBack

 

After the form is created, run it.

 

After a user successfully logs on to the system, we need to obtain the username after successful logon and pass it into the username text box on the password modification interface. Therefore, the FrmLogin type is logged on. we need to make some modifications in cs.

1. define a global variable Uid to obtain the user name for successful logon.

Public static string Uid; (Added to the following code)

// Connect to the configuration file to create an App. config // Of course, The ConfigurationManager system does not use its namespace by default. Therefore, you need to parse string connStr = ConfigurationManager. connectionStrings ["str"]. connectionString; // defines a global variable Uid; // obtains the username public static string Uid after successful logon;

 

2. After checking that the logon is successful, obtain the username entered in the current text box.

Uid = txtName. Text;

// If the entered password = password in the database if (pwd = txtPwd. text) {// obtain the user ID Uid = txtName after successful login. text; // indicates that the password under this account is correct and MessageBox is successfully logged on to the system. show ("the system is successfully logged on. You are redirected to the home page... "); // FrmManager manager = new FrmManager (); // manager. show (); // this. hide (); FrmMain main = new FrmMain (); main. show (); this. hide ();}

 

3. When the password modification form FrmPwdChange is loaded, the Uid attribute of the login form FrmLogin is automatically obtained, and the TXT username text box is assigned to the password modification form FrmPwdChange (the Code is as follows)

// Obtain the username private void FrmPwdChange_Load (object sender, EventArgs e) of the logon FORM {// obtain the public variable Uid string currentUser = FrmLogin after successful logon to the form. uid; // assign the obtained string currentUser to the txtUsername text box txtUsername. text = currentUser ;}

 

In this way, every time we log on successfully, jump to the password modification form, the user name text box will directly display our current login user name.

 

Run to see, we use the admin1 account to log on.

 

In the password modification form, username admin1 is automatically displayed in the text box where the username is located.

 

Next we will implement the complete code for modifying the password form.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. data. sqlClient; using System. configuration; /*************************************** ******************* Author: soFlash-blog Park http://www.cnblogs.com/longwu ** Description: FrmPwdChange. the cs password modification form is used to modify the personal password **************** for administrators and common users *************** ***************************************/ Namespace movie record management system {public partial class FrmPwdChange: form {public FrmPwdChange () {InitializeComponent ();} // obtain the connection path in the configuration file using the connection string static string connStr = ConfigurationManager. connectionStrings ["str"]. connectionString; // modify the password private void btnOK_Click (object sender, EventArgs e) {// use the SQL connection object to obtain the connection string SqlConnection conn = new SqlConnection (connStr); // use the SQL statement Obtain the password string SQL = "select MUserPwd from MovieAccount where MUserName = '" + txtUsername. text + "'"; // use the SQLcommand command to load the SQL connection string and the conn connection object SqlCommand cmd = new SqlCommand (SQL, conn); // open the conn connection conn. open (); // use an SQL data reader to obtain the data read in the cmd command SqlDataReader sdr = cmd. executeReader (); // if the SQL data reader reads the data if (sdr. read () {// assign the password under the username to the string oldPwd = sdr. getString (0 ). trim (); // if the password entered in the text box is The string oldPwd if (oldPwd = txtOldPwd. text) {// if the new password is null if (txtNewPwd. text. trim () = "" | txtNewPwdConfirm. text. trim () = "") {// The MessageBox prompt is displayed. show ("the new password cannot be blank! "); Return;} // continue to judge if two new passwords are different else if (txtNewPwd. Text. Trim ()! = TxtNewPwdConfirm. Text. Trim () {// the pop-up prompt shows that the new password is different for two times. Enter MessageBox. Show ("the new password entered twice is different. Please enter it again! "); // Clear txtNewPwd in the prompt box for the new password and re-proof password. text = ""; txtNewPwdConfirm. text = ""; return;} else {// if the new password is the same twice, close SqlDataReader to connect to sdr. close (); // execute the SQL statement string sqlUpdate = "update MovieAccount set MUserPwd = '" + txtNewPwdConfirm. text + "'where MUserName = '" + txtUsername. text + "'"; // a new database update command SqlCommand cmdUp = new SqlCommand (sqlUpdate, conn ); // if the number of rows obtained by cmdUp is 0, it indicates that the system update command has an exception if (cmdUp. executeNonQuery () = 0) {// system error MessageBox. Show ("Unknown error! "); Return;} else {// otherwise, MessageBox. Show (" Congratulations! Password modified! ") ;}} Else {// The old password cannot be blank or the MessageBox is incorrect. show ("the old password is incorrect or cannot be blank"); // The old password, new password, new password verification, and txtOldPwd are cleared at the same time. text = ""; txtNewPwd. text = ""; txtNewPwdConfirm. text = ""; return ;}} else {// if the user name is not read, the system prompts that the user name does not exist! MessageBox. Show ("the user name does not exist. Please enter it again! "); // Use the username, old password, new password, and new password for verification to clear txtUsername. text = ""; txtOldPwd. text = ""; txtNewPwd. text = ""; txtNewPwdConfirm. text = ""; return;} // closes the database connection conn. close () ;}// return the main form private void btnBack_Click (object sender, EventArgs e) {// FrmMain main = new FrmMain (); main. show (); this. close () ;}// get the username of the logon form private void FrmPwdChange_Load (object sender, EventArgs e) {// get the public variable Uid string currentUser = FrmLogin after successful logon to the form. uid; // assign the obtained string currentUser to the txtUsername text box txtUsername. text = currentUser ;}}}

 

Performance test:

First, use the admin1 account to log on to the system with a password of 666666.

 

When an incorrect old password is entered, the system prompts "the old password is incorrect or cannot be blank ".

 

Enter the correct password, but the new password and new password are blank. The prompt "new password cannot be blank" is displayed ".

Enter the new password in the new password box, and enter the new password again. The new password cannot be blank ".

 

The new password and new password are different. The prompt "two new passwords are different" is displayed ".

 

When the new password and new password are confirmed to be the same, click Modify. The prompt box "modified successfully" is displayed ".

 

If you need to set the password input box to invisible, replace it with the "*" symbol.

You can add the * sign in the PasswordChar attribute of the text box.

 

Effect display

 

In this way, the main interface and password modification interface of the movie record management system are completed. In the next phase, we will learn how to record and view the user's historical logon and exit conditions (view logs ).

 

Add source code

MovieRecordManagementSystem03.zip

Related Recommendations [click here to view the Directory]
  • C # simple movie record management system: developer 1 [user logon]
  • C # simple movie record management system: Development 2 [add, delete, modify, and query]
  • C # simple movie record management system: Development 4 [log view]
  • C # simple movie record management system: developer 5 [User Registration]
  • C # simple movie record management system: Development 6 [data search]
  • C # simple movie record management system: Development 7 [User Classification]
  • C # simple movie record management system: Development 8 [data backup]
  • C # simple movie record management system: Development 9 [data recovery]
  • C # simple movie record management system: Conclusion [permission Assignment]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.