-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsSQLStatements.cs
236 lines (207 loc) · 9.11 KB
/
clsSQLStatements.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
using Group2_3280_Invoice;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CS3280GroupProject
{
class clsSQLStatements
{
clsDataAccess db; // clsDataAccess object to be used to submit SQL statements to the database
DataSet ds; // Dataset to hold the returned data from queries
/// <summary>
/// Returns the full list of jewelry items from the database
/// </summary>
/// <returns></returns>
public ObservableCollection<clsItem> itemsCollection()
{
try
{
db = new clsDataAccess();
ObservableCollection<clsItem> col_Items = new ObservableCollection<clsItem>();
string sSQL; //Holds an SQL statement
int iRet = 0; //Number of return values
ds = new DataSet(); //Holds the return values
clsItem items; //Used to load the return values into the combo box
sSQL = "SELECT ItemCode, ItemDesc, Cost " + "FROM ItemDesc";
ds = db.ExecuteSQLStatement(sSQL, ref iRet);
//Creates item objects based on the data pulled from the query than adds the object to a list
for (int i = 0; i < iRet; i++)
{
items = new clsItem();
items.ItemCode = ds.Tables[0].Rows[i][0].ToString();
items.ItemDesc = ds.Tables[0].Rows[i]["ItemDesc"].ToString();
items.Cost = ds.Tables[0].Rows[i]["Cost"].ToString();
col_Items.Add(items);
}
return col_Items;
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
/// <summary>
/// Returns the full list of invoices from the database
/// </summary>
/// <returns></returns>
public ObservableCollection<clsInvoice> invoicesCollection()
{
try
{
string sSQL; //Holds an SQL statement
int iRet = 0; //Number of return values
ds = new DataSet();
db = new clsDataAccess();
ObservableCollection<clsInvoice> col_Invoices = new ObservableCollection<clsInvoice>();
clsInvoice invoice;
sSQL = "SELECT InvoiceNum, InvoiceDate, TotalCharge " +
"FROM Invoices";
ds = db.ExecuteSQLStatement(sSQL, ref iRet);
for (int i = 0; i < iRet; i++)
{
invoice = new clsInvoice();
invoice.InvoiceNum = ds.Tables[0].Rows[i][0].ToString();
invoice.InvoiceDate = ds.Tables[0].Rows[i]["InvoiceDate"].ToString();
invoice.TotalCharge = ds.Tables[0].Rows[i]["TotalCharge"].ToString();
col_Invoices.Add(invoice);
}
return col_Invoices;
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
/// <summary>
/// Adds a new invoice to the database
/// </summary>
/// <param name="date"></param>
/// <param name="totalCharge"></param>
/// <param name="items"></param>
public void addInvoice(string date, string totalCharge, ObservableCollection<clsItem> items)
{
try
{
db = new clsDataAccess();
int iRet = 0;
string sSQL = "INSERT INTO Invoices( InvoiceDate, TotalCharge) VALUES(#" +
date + "#, " + totalCharge + ")";
db.ExecuteNonQuery(sSQL);
string sSQLInvoiceNumber = "SELECT MAX(InvoiceNum) FROM Invoices";
DataSet newInvoice = db.ExecuteSQLStatement(sSQLInvoiceNumber, ref iRet);
string invoiceNumber = newInvoice.Tables[0].Rows[0][0].ToString();
int i = 1;
foreach (clsItem item in items)
{
string sSQLLineItem = "INSERT INTO LineItems(InvoiceNum, LineItemNum, ItemCode) " +
"VALUES('" + invoiceNumber + "', '" + i + "' , '" + item.ItemCode + "')";
db.ExecuteNonQuery(sSQLLineItem);
i += 1;
}
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
/// <summary>
/// Updates an existing invoice in the database
/// </summary>
/// <param name="invoiceNum"></param>
/// <param name="totalCharge"></param>
/// <param name="items"></param>
public void updateInvoice(string invoiceNum, string totalCharge, ObservableCollection<clsItem> items)
{
try
{
db = new clsDataAccess();
string sSQL = "UPDATE Invoices " +
"SET TotalCharge = '" + totalCharge +
"' WHERE InvoiceNum = " + invoiceNum;
db.ExecuteNonQuery(sSQL);
sSQL = "Delete FROM LineItems WHERE InvoiceNum = " + invoiceNum;
db.ExecuteNonQuery(sSQL);
int i = 1;
foreach (clsItem item in items)
{
string sSQLLineItem = "INSERT INTO LineItems(InvoiceNum, LineItemNum, ItemCode) " +
"VALUES('" + invoiceNum + "', '" + i + "' , '" + item.ItemCode + "')";
db.ExecuteNonQuery(sSQLLineItem);
i += 1;
}
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
/// <summary>
/// Returns the collection of items from a certain invoice
/// </summary>
/// <param name="invoiceNumber"></param>
/// <returns></returns>
public ObservableCollection<clsItem> invoiceItems(string invoiceNumber)
{
try
{
db = new clsDataAccess();
ObservableCollection<clsItem> col_Items = new ObservableCollection<clsItem>();
List<string> itemCode = new List<string>();
string sSQL; //Holds an SQL statement
int iRet = 0; //Number of return values
ds = new DataSet(); //Holds the return values
clsItem items; //Used to load the return values into the combo box
sSQL = "SELECT ItemCode FROM LineItems "
+ "WHERE InvoiceNum = " + invoiceNumber;
ds = db.ExecuteSQLStatement(sSQL, ref iRet);
// adds all of the different item codes on the invoice to a list
for (int i = 0; i < iRet; i++)
{
itemCode.Add(ds.Tables[0].Rows[i]["ItemCode"].ToString());
}
if (itemCode.Count > 0)
{
foreach (string code in itemCode)
{
sSQL = "SELECT ItemCode, ItemDesc, Cost FROM ItemDesc "
+ "WHERE ItemCode = '" + code + "'";
ds = db.ExecuteSQLStatement(sSQL, ref iRet);
items = new clsItem();
items.ItemCode = ds.Tables[0].Rows[0][0].ToString();
items.ItemDesc = ds.Tables[0].Rows[0]["ItemDesc"].ToString();
items.Cost = ds.Tables[0].Rows[0]["Cost"].ToString();
col_Items.Add(items);
}
}
return col_Items;
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
/// <summary>
/// Deletes an invoice from the database
/// </summary>
/// <param name="invoiceNum"></param>
public void deleteInvoice(string invoiceNum)
{
try
{
string sSQL = "Delete FROM LineItems WHERE InvoiceNum = " + invoiceNum;
db.ExecuteNonQuery(sSQL);
sSQL = "Delete FROM Invoices WHERE InvoiceNum = " + invoiceNum;
db.ExecuteNonQuery(sSQL);
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
}
}