-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResetPassword.cs
113 lines (96 loc) · 3.56 KB
/
ResetPassword.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
namespace Coders_Space
{
public partial class ResetPassword : Form
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
string emailToReset;
public ResetPassword(string emailToReset)
{
InitializeComponent();
this.emailToReset = emailToReset;
}
private void guna2Button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void loginBtn_Click(object sender, EventArgs e)
{
Login login = new Login();
login.ShowDialog();
this.Close();
}
private void changepasswordBtn_Click(object sender, EventArgs e)
{
try
{
string newPassword = textBoxNewPassword.Text;
string confirmPassword = textBoxConfirmNewPassword.Text;
if (IsEmailExist(this.emailToReset))
{
if (newPassword == confirmPassword)
{
UpdatePassword(emailToReset, newPassword);
MessageBox.Show("Password reset successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
Login login = new Login();
login.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("New password and confirm password do not match. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Email not found. Please enter a valid email.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdatePassword(string email, string newPassword)
{
using (SqlConnection con = new SqlConnection(cs))
{
string query = "UPDATE USERS SET PASSWORD = @NewPassword WHERE EMAIL = @Email";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@NewPassword", newPassword);
cmd.Parameters.AddWithValue("@Email", email);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
private bool IsEmailExist(string email)
{
using (SqlConnection con = new SqlConnection(cs))
{
string query = "SELECT COUNT(*) FROM USERS WHERE EMAIL = @Email";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Email", email);
con.Open();
int count = (int)cmd.ExecuteScalar();
con.Close();
return count > 0;
}
}
private void minimizeBTN_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
}