How to Create Complete Forgot Password System in C#.NET using SQL Server Database?
If you are planning to create a project in C#.NET, then you have to have login and forgot password system in your system. In this tutorial i am going to show you how you can create a complete login and forgot password system in C# using SQL Server Database.
Topics covered by this video:
1. How to create Login form in c#.
2. How to send an email from c#.
3. How to update database from c#.
4. How to access variable from one form to another form.
STEPS:
[sociallocker]
1. Open visual studio go to file new>project and from the left side of the window, select C#>Windows and from the middle part select windows form application and then give the name of your project and click OK.
2. Now drag and drop three label, two TextBox and one Button. change the properties( Name of the textbox[txtUser, txtPass], text of the button and label as show in following pictures.[to underline the label text find font properties under this find underline properties and set it to true] [To change the cursor from pointer to hand, find cursor properties and change it to hand]
3. Now add two more form and give them sendCode and ResetPassword as:
3. Now double click that forgot password label and write the following code:
sendCode sc = new sendCode(); this.Hide(); sc.Show();
4. Now sendCode form will be display and from the toolbox drag and drop two label, two button and two textbox over the form and change their properties(Name of the textbox[txtEmail andd txtVerCode] and Text of label and button) as shown in following picture.
How to Create Forgot Password and Login form in C#?5. Now double click that Send button to go to the cs file.
a. Move to the top of that file to import some namespace as:
using System.Net; using System.Net.Mail;
b. Now inside the class define following variable:
string randomCode; public static string to;
c. Now inside the click event of that send button write the following code:
string from, pass, messageBody; Random rand = new Random(); randomCode = (rand.Next(999999)).ToString(); MailMessage message = new MailMessage(); to = (txtEmail.Text).ToString(); from = "YOUR_EMAIL"; pass = "YOUR_EMAIL_PASSWORD"; messageBody = "your reset code is " + randomCode; message.To.Add(to); message.From = new MailAddress(from); message.Body = messageBody; message.Subject = "password reseting code"; SmtpClient smtp = new SmtpClient("smtp.gmail.com"); smtp.EnableSsl = true; smtp.Port = 587; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(from, pass); try { smtp.Send(message); MessageBox.Show("code send successfully"+randomCode); } catch (Exception ex) { MessageBox.Show(ex.Message); }
d. Now go back to the designing of this sendCode form and double click that verify code button and write the following code:
if (randomCode == (txtVerCode.Text).ToString()) { to = txtEmail.Text; ResetPassword rp = new ResetPassword(); this.Hide(); rp.Show(); } else { MessageBox.Show("wrong code"); }
6. When the user enter code is match with the code that we send to their account ResetPassword form will open. Now drag and drop two label, two textbox and two button over the form. Change the properties of each control(Name of the TextBox[txtResetPass, txtResetPassVer], Text of Button and Label). Which looks like this:
7. Now we have to create database in SQL Server Management Studio. To create a database and table in SQL Server Database, open SQL Server application, and Right click over Database and then click new, give the database name whatever you want and click OK. After then expand that Database directory which you can see in the left pane of the SQL Server Studio Software and expand that Database directory and then expand your recently created Database. And then you will see Table directory. Right click that Table Directory click on Table and click OK. You will see the new window which looks like following picture after filling it:
Save that table by pressing ctrl+s and give the table name whatever you want.
And then Right click your Table and then select Edit top 200 Rows click OK and then write the username, password (like example@example.com, admin) and click close button; this will save the username password and user type in your table.
8. We need to connect our application to the database for this, click on the Server Explore which is in the right side of the Toolbox in the left pane of your visual studio. And then right click on the Data Connection>Add connection. New window will appear select your Data Source; in our Case Data Source is Microsoft SQL Server. To do this click on change and select Microsoft SQL Server, Click OK. And then New window will appear, select your Server Name your Database Name and click OK.
9. Now double click that reset button to go to the cs file.
a. At the top of this cs file, import some namespace like:
using System.Data; using System.Data.SqlClient;
b. Now inside the class define one variable which store the value that we send from sendCode form:
string username = sendCode.to;
b. now inside the clicking event of reset button write the following code.
if (txtResetPass.Text == txtResetPassVer.Text) { SqlConnection con = new SqlConnection("Data Source=RAN-PC;Initial Catalog=forgetPassword;Integrated Security=True"); SqlCommand cmd = new SqlCommand("UPDATE YOUR_TABLE_NAME SET [password] = '" + txtResetPassVer.Text + "' WHERE username='" + username + "' ", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("reset successfully"); } else { MessageBox.Show("the new password do not match so enter same password"); }
NOTE: Change this code in your context (because your connection string (Data Source=RAN-PC;Initial Catalog=forgetPassword;Integrated Security=True) is different than mine)
10. Again go back to designing fo this ResetPassword form and double click that login button and write the following code because when the user click that we need to redirect the user to to login form and our login form name is Form1.
Form1 f = new Form1(); this.Hide(); f.Show();
11. Now go to the Login form design and double click that Login button to go to the cs file.
a. We need to import some namespace like:
using System.Data; using System.Data.SqlClient;
b. Now inside the clicking event of the login button write the following code:
SqlConnection con = new SqlConnection("Data Source=RAN-PC;Initial Catalog=forgetPassword;Integrated Security=True"); SqlDataAdapter sda = new SqlDataAdapter("select * from forgotPassword where username='" + txtUser.Text + "' and password='" + txtPass.Text + "'", con); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows.Count > 0) { MessageBox.Show("login successfully"); } else { MessageBox.Show("error"); }
12. Now you have to on less secure apps in your gmail account so go to this link to enable it.
https://myaccount.google.com/lesssecureapps
13. Now run your application by clicking that run button, click forgot password; you will redirect to the sendCode form; enter the email in which you want to send the code and then click send button. After few second code will be received in that email. Enter that code and click verify button after then you will see ResetPassword form. Now enter new password and click that reset button password will reset. Now to login to the system click that login button. Login form will appear and then enter username and password and click Login. That’s it.
[/sociallocker]
if you have any confusion, feel free to watch the video: