-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateCust.cs
253 lines (207 loc) · 8.03 KB
/
UpdateCust.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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 MySql.Data.MySqlClient;
using WGUC969Project.Database;
using System.Globalization;
using System.Text.RegularExpressions;
namespace WGUC969Project
{
public partial class UpdateCust : Form
{
//globals
public string firstName;
public string lastName;
public string customerAddress;
public string customerCity;
public string customerCountry;
public string finalNameForDB;
public string addCustStr;
public string phoneNumber;
//relevent Keys of selected customer and associated tables
//custID <-> addressId <-> cityId <-> countryId
public int dbCustIdglobal;
public int countryTableId;
public int cityTableId;
public int addressTableId;
//commands needed for editing data
public string addCountryCmd;
public string addCityCmd;
public string addAddressCmd;
private Form1 _form1;
public UpdateCust()
{
InitializeComponent();
}
public UpdateCust(int dbCustid, int addressIdKey, int cityIdKey, int countryIdKey, Form1 form1)
{
InitializeComponent();
dbCustIdglobal = dbCustid;
addressTableId = addressIdKey;
cityTableId = cityIdKey;
countryTableId = countryIdKey;
_form1 = form1;
//get customer name
string getFname = "select customerName from customer where customerId = " + dbCustid;
MySqlCommand sqlConvert1 = new MySqlCommand(getFname, DBConnection.conn);
finalNameForDB = (string)sqlConvert1.ExecuteScalar();
string[]fullName = finalNameForDB.Split(' ');
UpdateCustFirstNameTextInp.Text = fullName[0];
UpdateCustLastNameTextInp.Text = fullName[1];
//get customer address
string getAddress = "select address from address where addressId = " + addressIdKey;
MySqlCommand sqlConvert2 = new MySqlCommand(getAddress, DBConnection.conn);
customerAddress = (string)sqlConvert2.ExecuteScalar();
UpdateCustAddressTextInp.Text = customerAddress;
//get customer city
string getCity = "select city from city where cityId = " + cityIdKey;
MySqlCommand sqlConvert3 = new MySqlCommand(getCity, DBConnection.conn);
customerCity = (string)sqlConvert3.ExecuteScalar();
UpdCustCityTextInput.Text = customerCity;
//get customer country
string getCountry = "select country from country where countryId = " + countryIdKey;
MySqlCommand sqlConvert4 = new MySqlCommand(getCountry, DBConnection.conn);
customerCountry = (string)sqlConvert4.ExecuteScalar();
UpdCustCountryTextInput.Text = customerCountry;
//get customer phone
string getPhoneNumber = "select phone from address where addressId = " + addressIdKey;
MySqlCommand sqlConvert5 = new MySqlCommand(getPhoneNumber, DBConnection.conn);
phoneNumber = (string)sqlConvert5.ExecuteScalar();
UpdCustPhoneTextInput.Text = phoneNumber;
}
private void UpdateCust_Load(object sender, EventArgs e)
{
}
private void UpdateCustFirstNameTextInp_TextChanged(object sender, EventArgs e)
{
if(!Regex.IsMatch(UpdateCustFirstNameTextInp.Text, @"^[a-zA-Z\s]+$") && UpdateCustFirstNameTextInp.Text.Length > 0)
{
UpdateCustFirstNameTextInp.BackColor = Color.Chocolate;
MessageBox.Show("Letters Only");
UpdateCustFirstNameTextInp.Text = "";
}
else
{
UpdateCustFirstNameTextInp.BackColor = (UpdateCustFirstNameTextInp.Text.Length == 0) ? Color.Chocolate : Color.White;
firstName = UpdateCustFirstNameTextInp.Text;
}
//firstName = UpdateCustFirstNameTextInp.Text;
}
private void UpdateCustLastNameTextInp_TextChanged(object sender, EventArgs e)
{
if(!Regex.IsMatch(UpdateCustLastNameTextInp.Text, @"^[a-zA-Z\s]+$") && UpdateCustLastNameTextInp.Text.Length > 0)
{
UpdateCustLastNameTextInp.BackColor = Color.Chocolate;
MessageBox.Show("Letters Only");
UpdateCustLastNameTextInp.Text = "";
}
else
{
UpdateCustLastNameTextInp.BackColor = (UpdateCustLastNameTextInp.Text.Length == 0) ? Color.Chocolate : Color.White;
lastName = UpdateCustLastNameTextInp.Text;
}
//lastName = UpdateCustLastNameTextInp.Text;
}
private void UpdateCustAddressTextInp_TextChanged(object sender, EventArgs e)
{
UpdateCustAddressTextInp.BackColor = (UpdateCustAddressTextInp.Text.Length == 0) ? Color.Chocolate : Color.White;
customerAddress = UpdateCustAddressTextInp.Text;
}
private void UpdCustCityTextInput_TextChanged(object sender, EventArgs e)
{
if(!Regex.IsMatch(UpdCustCityTextInput.Text, @"^[a-zA-Z\s]+$") && UpdCustCityTextInput.Text.Length > 0)
{
UpdCustCityTextInput.BackColor = Color.Chocolate;
MessageBox.Show("Letters Only");
UpdCustCityTextInput.Text = "";
}
else
{
UpdCustCityTextInput.BackColor = (UpdCustCityTextInput.Text.Length == 0) ? Color.Chocolate : Color.White;
customerCity = UpdCustCityTextInput.Text;
}
customerCity = UpdCustCityTextInput.Text;
}
private void UpdCustCountryTextInput_TextChanged(object sender, EventArgs e)
{
if(!Regex.IsMatch(UpdCustCountryTextInput.Text, @"^[a-zA-Z\s]+$") && UpdCustCountryTextInput.Text.Length > 0)
{
UpdCustCountryTextInput.BackColor = Color.Chocolate;
MessageBox.Show("Letters Only");
UpdCustCountryTextInput.Text = "";
}
else
{
UpdCustCountryTextInput.BackColor = (UpdCustCountryTextInput.Text.Length == 0) ? Color.Chocolate : Color.White;
customerCountry = UpdCustCountryTextInput.Text;
}
customerCountry = UpdCustCountryTextInput.Text;
}
private void UpdCustPhoneTextInput_TextChanged(object sender, EventArgs e)
{
if(!Regex.IsMatch(UpdCustPhoneTextInput.Text, @"^[0-9]+$") && UpdCustPhoneTextInput.Text.Length > 0)
{
UpdCustPhoneTextInput.BackColor = Color.Chocolate;
MessageBox.Show("Numbers Only");
UpdCustPhoneTextInput.Text = "";
}
else
{
UpdCustPhoneTextInput.BackColor = (UpdCustPhoneTextInput.Text.Length == 0) ? Color.Chocolate : Color.White;
phoneNumber = UpdCustPhoneTextInput.Text;
}
phoneNumber = UpdCustPhoneTextInput.Text;
}
private void UpdateCustSubmitBttn_Click(object sender, EventArgs e)
{
if (
UpdateCustFirstNameTextInp.Text.Length == 0 ||
UpdateCustLastNameTextInp.Text.Length == 0 ||
UpdateCustAddressTextInp.Text.Length == 0 ||
UpdCustCityTextInput.Text.Length == 0 ||
UpdCustCountryTextInput.Text.Length == 0 ||
UpdCustPhoneTextInput.Text.Length == 0
)
{
MessageBox.Show("No fields can be left blank");
return;
}
string[] buildMe = {firstName,lastName};
finalNameForDB = string.Join(" ",buildMe);
//update finalName
string updName = "update customer set customerName = '" + finalNameForDB + "' where customerId = " + dbCustIdglobal + ";";
MySqlCommand updNameCmd = new MySqlCommand(updName, DBConnection.conn);
updNameCmd.ExecuteNonQuery();
//update address
string updAddress = "update address set address = '" + customerAddress + "' where addressId = " + addressTableId + ";";
MySqlCommand updAddressCmd = new MySqlCommand(updAddress, DBConnection.conn);
updAddressCmd.ExecuteNonQuery();
//update city
string updCity = "update city set city = '" + customerCity + "' where cityId = " + cityTableId + ";";
MySqlCommand updCityCmd = new MySqlCommand(updCity, DBConnection.conn);
updCityCmd.ExecuteNonQuery();
//update country
string updCountry = "update country set country = '" + customerCountry + "' where countryId = " + countryTableId + ";";
MySqlCommand updCountryCmd = new MySqlCommand(updCountry, DBConnection.conn);
updCountryCmd.ExecuteNonQuery();
//update phone
string updPhone = "update address set phone = '" + phoneNumber + "' where addressId = " + addressTableId + ";";
MySqlCommand updPhoneCmd = new MySqlCommand(updPhone, DBConnection.conn);
updPhoneCmd.ExecuteNonQuery();
//call updates on all DGV's
_form1.outerCallToRefresh();
//close window
this.Close();
}
private void UpdateCustCancelBttn_Click(object sender, EventArgs e)
{
}
}
}