-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewItemForm.cs
More file actions
219 lines (197 loc) · 8.07 KB
/
NewItemForm.cs
File metadata and controls
219 lines (197 loc) · 8.07 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
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
using Microsoft.Data.SqlClient;
using System;
using System.Buffers.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Managment
{
public partial class NewItemForm : Form
{
SqlConnection conn = new SqlConnection(StaticVariables.connectionString);
private DataGridViewRow values;
private string tableName;
private int rowId;
private int tableRows = 0;
private int formCategoryId;
public NewItemForm(int formCategory, DataGridViewRow rowValues = null, string tableId = "")
{
InitializeComponent();
formCategoryId = formCategory;
formMethod.Text = "New Item";
if (rowValues != null)
{
comboBox1.Enabled = false;
formMethod.Text = "Item Edit";
tableName = tableId;
values = rowValues;
FillTextBoxFields();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0 || comboBox1.SelectedIndex == 1)
{
tableName = (comboBox1.SelectedIndex == 0) ? StaticVariables.baseDB : StaticVariables.mattressDB;
basePanel.Visible = true;
basePanel.Enabled = true;
fabricPanel.Visible = false;
fabricPanel.Enabled = false;
}
else
{
tableName = StaticVariables.fabricDB;
basePanel.Visible = false;
basePanel.Enabled = false;
fabricPanel.Visible = true;
fabricPanel.Enabled = true;
}
}
private void sumbitButton_Click(object sender, EventArgs e)
{
if (formCategoryId == 0) //New Item Form
NewItemDBCommand();
else // Edit Item
EditItemDBCommand();
}
private async void NewItemDBCommand()
{
try
{
string sqlQuery = "";
if (tableName == StaticVariables.baseDB)
sqlQuery = $"INSERT INTO {tableName} (BASENAME, DIMENSIONX, DIMENSIONY, QUANTITY) " +
"VALUES (@newName, @newX, @newy, @newQuantity)";
else if (tableName == StaticVariables.mattressDB)
sqlQuery = $"INSERT INTO {tableName} (MATTRESSNAME, DIMENSIONX, DIMENSIONY, QUANTITY) " +
"VALUES (@newName, @newX, @newy, @newQuantity)";
else
sqlQuery = $"INSERT INTO {tableName} (FABRICNAME, METERS) VALUES (@newName, @newMeters)";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
await conn.OpenAsync();
if (tableName == StaticVariables.baseDB || tableName == StaticVariables.mattressDB)
{
cmd.Parameters.AddWithValue("@newName", itemName.Text);
cmd.Parameters.AddWithValue("@newX", itemDimensionX.Text);
cmd.Parameters.AddWithValue("@newy", itemDimensionY.Text);
cmd.Parameters.AddWithValue("@newQuantity", itemQuantity.Text);
}
else
{
cmd.Parameters.AddWithValue("@newName", fabricName.Text);
cmd.Parameters.AddWithValue("@newMeters", fabricMeter.Text);
}
cmd.ExecuteNonQuery();
}
tableRows++;
this.Close();
CostumMessageBox customMessageBox = new CostumMessageBox(true);
customMessageBox.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
CostumMessageBox customMessageBox = new CostumMessageBox(false, ex.Message);
customMessageBox.ShowDialog();
}
finally
{
if (conn.State == ConnectionState.Open)
{
await conn.CloseAsync();
}
}
}
private async void EditItemDBCommand()
{
try
{
string sqlQuery = "";
if (tableName == StaticVariables.baseDB)
sqlQuery = $"UPDATE {tableName} SET BASENAME = @newName, DIMENSIONX = @newX, DIMENSIONy = @newy" +
", QUANTITY = @newQuantity WHERE Id = @id";
else if (tableName == StaticVariables.mattressDB)
sqlQuery = $"UPDATE {tableName} SET MATTRESSNAME = @newName, DIMENSIONX = @newX, DIMENSIONy = @newy" +
", QUANTITY = @newQuantity WHERE Id = @id";
else
sqlQuery = $"UPDATE {tableName} SET FABRICNAME = @newName, METERS = @newMeters WHERE Id = @id";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
await conn.OpenAsync();
if (tableName == StaticVariables.baseDB || tableName == StaticVariables.mattressDB)
{
cmd.Parameters.AddWithValue("@newName", itemName.Text);
cmd.Parameters.AddWithValue("@newX", itemDimensionX.Text);
cmd.Parameters.AddWithValue("@newy", itemDimensionY.Text);
cmd.Parameters.AddWithValue("@newQuantity", itemQuantity.Text);
cmd.Parameters.AddWithValue("@id", rowId);
}
else
{
cmd.Parameters.AddWithValue("@newName", fabricName.Text);
cmd.Parameters.AddWithValue("@newMeters", fabricMeter.Text);
cmd.Parameters.AddWithValue("@id", rowId);
}
cmd.ExecuteNonQuery();
}
this.Close();
CostumMessageBox customMessageBox = new CostumMessageBox(true);
customMessageBox.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
CostumMessageBox customMessageBox = new CostumMessageBox(false, ex.Message);
customMessageBox.ShowDialog();
}
finally
{
if (conn.State == ConnectionState.Open)
{
await conn.CloseAsync();
}
}
}
private void FillTextBoxFields()
{
if (tableName == StaticVariables.baseDB || tableName == StaticVariables.mattressDB)
{
basePanel.Visible = true;
basePanel.Enabled = true;
fabricPanel.Visible = false;
rowId = (int)values.Cells[0].Value;
itemName.Text = values.Cells[1].Value.ToString();
itemDimensionX.Text = values.Cells[2].Value.ToString();
itemDimensionY.Text = values.Cells[3].Value.ToString();
itemQuantity.Text = values.Cells[4].Value.ToString();
}
else
{
basePanel.Visible = false;
fabricPanel.Visible = true;
fabricPanel.Enabled = true;
rowId = (int)values.Cells[0].Value;
fabricName.Text = values.Cells[1].Value.ToString();
fabricMeter.Text = values.Cells[2].Value.ToString();
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void itemName_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsLetter(e.KeyChar))
{
e.Handled = true;
}
}
}
}