-
Notifications
You must be signed in to change notification settings - Fork 1
/
Profile.cs
137 lines (120 loc) · 4.45 KB
/
Profile.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 Profile : Form
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
string userEmail;
public Profile(string email)
{
InitializeComponent();
userEmail = email;
LoadUserData(userEmail);
}
private string GetSubscriptionDetails(string userId)
{
string subscriptionDetails = "Unlimited (-1)";
SqlConnection con = new SqlConnection(cs);
try
{
con.Open();
string query = "SELECT TOP 1 * FROM Transactions WHERE USERID = @userId ORDER BY TRANSACTION_DATE DESC";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@userId", userId);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
int subValidMonth = (int)reader["SUB_VALID_MONTH"];
if (subValidMonth != -1)
{
DateTime transactionDate = (DateTime)reader["TRANSACTION_DATE"];
DateTime expiryDate = transactionDate.AddMonths(subValidMonth);
int daysLeft = (int)(expiryDate - DateTime.Now).TotalDays;
if (daysLeft >= 0)
{
subscriptionDetails = $"{daysLeft} days left";
}
else
{
subscriptionDetails = "Expired";
}
}
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error fetching subscription details: " + ex.Message);
}
finally
{
con.Close();
}
return subscriptionDetails;
}
private void LoadUserData(string email)
{
SqlConnection con = new SqlConnection(cs);
try
{
con.Open();
string query = "SELECT * FROM USERS WHERE email = @email";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@email", email);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
if (reader["PHOTO"] != DBNull.Value)
{
byte[] imageData = (byte[])reader["PHOTO"];
MemoryStream ms = new MemoryStream(imageData);
pictureBoxUser.Image = Image.FromStream(ms);
}
labelName.Text = reader["NAME"].ToString();
labelName.TextAlignment = ContentAlignment.MiddleCenter;
labelContactNo.Text = reader["CONTACT_NO"].ToString();
labelEmail.Text = reader["EMAIL"].ToString();
labelDOB.Text = ((DateTime)reader["DOB"]).ToShortDateString();
labelGender.Text = reader["GENDER"].ToString();
labelUserType.Text = reader["ROLE"].ToString();
string userId = reader["ID"].ToString();
string subscriptionDetails = GetSubscriptionDetails(userId);
labelSub.Text = subscriptionDetails;
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
con.Close();
}
}
private void editProfileBtn_Click(object sender, EventArgs e)
{
using (EditProfile editProfile = new EditProfile(userEmail))
{
editProfile.ShowDialog();
}
}
private void changePasswordBtn_Click(object sender, EventArgs e)
{
using (ChangePassword changePassword = new ChangePassword(userEmail))
{
changePassword.ShowDialog();
}
}
}
}