-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm_SignUp.cs
More file actions
178 lines (153 loc) · 8.66 KB
/
Form_SignUp.cs
File metadata and controls
178 lines (153 loc) · 8.66 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Npgsql;
using System.Transactions;
namespace ExerciseApp
{
public partial class Form_SignUp : Form
{
string dbConnection = ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString;
public Form_SignUp()
{
InitializeComponent();
}
private void Form_SignUp_Load(object sender, EventArgs e)
{
}
private void button_CreateAccount_Click(object sender, EventArgs e)
{
string enteredfName = textBox_fName.Text;
string enteredlName = textBox_lName.Text;
string enteredEmail = textBox_Email.Text;
string enteredPassword = textBox_Password.Text;
string enteredPasswordConfirm = textBox_ConfirmPswd.Text;
string enteredUsername = textBox_Username.Text;
string enteredGender = comboBox_Gender.Text;
string enteredAge = textBox_Age.Text;
string enteredWeight = textBox_Weight.Text;
string enteredHeight = textBox_Height.Text;
if (string.Compare(enteredPassword, enteredPasswordConfirm) != 0)
{
MessageBox.Show("Passwords do not match.");
return;
}
if (string.IsNullOrEmpty(enteredEmail) || string.IsNullOrEmpty(enteredPassword)
|| string.IsNullOrEmpty(enteredUsername) || string.IsNullOrEmpty(enteredPasswordConfirm))
{
MessageBox.Show("Please enter information into the required(*) fields.");
return;
}
using (NpgsqlConnection connection = new NpgsqlConnection(dbConnection))
{
connection.Open();
// Begin a transaction as code inserts values into two tables
using (NpgsqlTransaction transaction = connection.BeginTransaction())
{
try
{
string checkExistingAccount = @"SELECT * FROM users WHERE email = @email";
// Check for existing account
using (NpgsqlCommand checkExistingAccountcommand = new NpgsqlCommand(checkExistingAccount, connection))
{
checkExistingAccountcommand.Parameters.AddWithValue("@email", enteredEmail);
NpgsqlDataReader reader = checkExistingAccountcommand.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("An account with that email already exists!");
reader.Close();
return;
}
reader.Close();
}
// Insert into Users table
string insertUserQuery = @"INSERT INTO users (email, username, password, fName, lName)
VALUES (@email, @username, @password, @fName, @lName)
RETURNING user_id";
// Execute insert command for Users table
int userId;
using (NpgsqlCommand command = new NpgsqlCommand(insertUserQuery, connection))
{
command.Parameters.AddWithValue("@email", enteredEmail);
command.Parameters.AddWithValue("@username", enteredUsername);
command.Parameters.AddWithValue("@password", enteredPassword);
command.Parameters.AddWithValue("@fName", string.IsNullOrEmpty(enteredfName) ? (object)DBNull.Value : enteredfName);
command.Parameters.AddWithValue("@lName", string.IsNullOrEmpty(enteredlName) ? (object)DBNull.Value : enteredlName);
// Execute the command and retrieve the userid of the newly inserted user
userId = (int)command.ExecuteScalar();
if (userId == 0)
{
throw new Exception("Failed to add user information.");
}
// Insert into UserPhysicalDetails table
string insertUserPhysicalDetailsQuery = @"INSERT INTO user_physical_details (user_id, age, gender, height)
VALUES (@userId, @age, @gender, @height)";
// Execute insert command for UserPhysicalDetails table
using (NpgsqlCommand insertUserPhysicalDetailsCommand = new NpgsqlCommand(insertUserPhysicalDetailsQuery, connection))
{
insertUserPhysicalDetailsCommand.Parameters.AddWithValue("@userId", userId);
insertUserPhysicalDetailsCommand.Parameters.AddWithValue("@gender", string.IsNullOrEmpty(enteredGender)
? "Other" : enteredGender);
// Validate certain inputs to avoid insetion errors
try
{
insertUserPhysicalDetailsCommand.Parameters.AddWithValue("@age", string.IsNullOrEmpty(enteredAge)
? (object)DBNull.Value : int.Parse(enteredAge));
}
catch (FormatException) { throw new Exception("Enter a valid age."); }
try
{
insertUserPhysicalDetailsCommand.Parameters.AddWithValue("@height", string.IsNullOrEmpty(enteredHeight)
? (object)DBNull.Value : decimal.Parse(enteredHeight));
}
catch (FormatException) { throw new Exception("Enter a valid height."); }
int rowsAffected = insertUserPhysicalDetailsCommand.ExecuteNonQuery();
if (rowsAffected == 0)
{
throw new Exception("Failed to add user physical details.");
}
}
}
// Insert into Weight table
string insertWeightQuery = @"INSERT INTO weight (user_id, weight, date)
VALUES (@userId, @weight, CURRENT_TIMESTAMP)";
// Execute insert command for Weight table
using (NpgsqlCommand insertWeightCommand = new NpgsqlCommand(insertWeightQuery, connection))
{
insertWeightCommand.Parameters.AddWithValue("@userId", userId);
try
{
insertWeightCommand.Parameters.AddWithValue("@weight", string.IsNullOrEmpty(enteredWeight)
? (object)DBNull.Value : decimal.Parse(enteredHeight));
}
catch (FormatException) { throw new Exception("Enter a valid weight."); }
int rowsAffected = insertWeightCommand.ExecuteNonQuery();
if (rowsAffected == 0)
{
throw new Exception("Failed to insert weight.");
}
}
// Commit the transaction if all operations succeed
transaction.Commit();
MessageBox.Show("Account created successfully!");
}
catch (Exception ex)
{
// Rollback the transaction if an exception occurs during any operation
transaction.Rollback();
MessageBox.Show("Error creating account: " + ex.Message);
return;
}
}
}
this.Close();
}
}
}