-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChangePassword.cs
84 lines (76 loc) · 2.8 KB
/
ChangePassword.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Coders_Space
{
public partial class ChangePassword : Form
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
string userEmail;
public ChangePassword(string email)
{
InitializeComponent();
userEmail = email;
}
private void changepasswordBtn_Click(object sender, EventArgs e)
{
string oldPassword = textBoxOldPassword.Text;
string newPassword = textBoxNewPassword.Text;
string confirmPassword = textBoxConfirmNewPassword.Text;
if (string.IsNullOrEmpty(oldPassword) || string.IsNullOrEmpty(newPassword) || string.IsNullOrEmpty(confirmPassword))
{
MessageBox.Show("Please fill in all fields.");
return;
}
if (newPassword != confirmPassword)
{
MessageBox.Show("New password and confirm password do not match.");
return;
}
UpdatePassword(userEmail, oldPassword, newPassword);
}
private void UpdatePassword(string email, string oldPassword, string newPassword)
{
using (SqlConnection con = new SqlConnection(cs))
{
try
{
con.Open();
string query = "UPDATE USERS SET PASSWORD = @NewPassword WHERE EMAIL = @Email AND PASSWORD = @OldPassword";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Email", email);
cmd.Parameters.AddWithValue("@OldPassword", oldPassword);
cmd.Parameters.AddWithValue("@NewPassword", newPassword);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Password changed successfully.");
textBoxOldPassword.Clear();
textBoxNewPassword.Clear();
textBoxConfirmNewPassword.Clear();
}
else
{
MessageBox.Show("Invalid old password. Password not changed.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
private void exitBTN_Click(object sender, EventArgs e)
{
this.Close();
}
}
}