PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/e-business/Gallery/App_Code/OrdersAccess.cs

https://github.com/marvellouz/fmi_projects
C# | 285 lines | 198 code | 18 blank | 69 comment | 2 complexity | 96b64a208fb2a1547cf5b1856929cb9b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Data.Common;
  7. /// <summary>
  8. /// Wraps order data
  9. /// </summary>
  10. public struct OrderInfo
  11. {
  12. public int OrderID;
  13. public decimal TotalAmount;
  14. public string DateCreated;
  15. public string DateShipped;
  16. public bool Verified;
  17. public bool Completed;
  18. public bool Canceled;
  19. public string Comments;
  20. public string CustomerName;
  21. public string ShippingAddress;
  22. public string CustomerEmail;
  23. }
  24. /// <summary>
  25. /// Summary description for OrdersAccess
  26. /// </summary>
  27. public class OrdersAccess
  28. {
  29. public OrdersAccess()
  30. {
  31. //
  32. // TODO: Add constructor logic here
  33. //
  34. }
  35. // Retrieve the recent orders
  36. public static DataTable GetByRecent(int count)
  37. {
  38. // get a configured DbCommand object
  39. DbCommand comm = GenericDataAccess.CreateCommand();
  40. // set the stored procedure name
  41. comm.CommandText = "OrdersGetByRecent";
  42. // create a new parameter
  43. DbParameter param = comm.CreateParameter();
  44. param.ParameterName = "@Count";
  45. param.Value = count;
  46. param.DbType = DbType.Int32;
  47. comm.Parameters.Add(param);
  48. // return the result table
  49. DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
  50. return table;
  51. }
  52. // Retrieve orders that have been placed in a specified period of time
  53. public static DataTable GetByDate(string startDate, string endDate)
  54. {
  55. // get a configured DbCommand object
  56. DbCommand comm = GenericDataAccess.CreateCommand();
  57. // set the stored procedure name
  58. comm.CommandText = "OrdersGetByDate";
  59. // create a new parameter
  60. DbParameter param = comm.CreateParameter();
  61. param.ParameterName = "@StartDate";
  62. param.Value = startDate;
  63. param.DbType = DbType.Date;
  64. comm.Parameters.Add(param);
  65. // create a new parameter
  66. param = comm.CreateParameter();
  67. param.ParameterName = "@EndDate";
  68. param.Value = endDate;
  69. param.DbType = DbType.Date;
  70. comm.Parameters.Add(param);
  71. // return the result table
  72. DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
  73. return table;
  74. }
  75. // Retrieve orders that need to be verified or canceled
  76. public static DataTable GetUnverifiedUncanceled()
  77. {
  78. // get a configured DbCommand object
  79. DbCommand comm = GenericDataAccess.CreateCommand();
  80. // set the stored procedure name
  81. comm.CommandText = "OrdersGetUnverifiedUncanceled";
  82. // return the result table
  83. DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
  84. return table;
  85. }
  86. // Retrieve orders that need to be shipped/completed
  87. public static DataTable GetVerifiedUncompleted()
  88. {
  89. // get a configured DbCommand object
  90. DbCommand comm = GenericDataAccess.CreateCommand();
  91. // set the stored procedure name
  92. comm.CommandText = "OrdersGetVerifiedUncompleted";
  93. // return the result table
  94. DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
  95. return table;
  96. }
  97. // Retrieve order information
  98. public static OrderInfo GetInfo(string orderID)
  99. {
  100. // get a configured DbCommand object
  101. DbCommand comm = GenericDataAccess.CreateCommand();
  102. // set the stored procedure name
  103. comm.CommandText = "OrderGetInfo";
  104. // create a new parameter
  105. DbParameter param = comm.CreateParameter();
  106. param.ParameterName = "@OrderID";
  107. param.Value = orderID;
  108. param.DbType = DbType.Int32;
  109. comm.Parameters.Add(param);
  110. // obtain the results
  111. DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
  112. DataRow orderRow = table.Rows[0];
  113. // save the results into an OrderInfo object
  114. OrderInfo orderInfo;
  115. orderInfo.OrderID = Int32.Parse(orderRow["OrderID"].ToString());
  116. orderInfo.TotalAmount = Decimal.Parse(orderRow["TotalAmount"].ToString());
  117. orderInfo.DateCreated = orderRow["DateCreated"].ToString();
  118. orderInfo.DateShipped = orderRow["DateShipped"].ToString();
  119. orderInfo.Verified = bool.Parse(orderRow["Verified"].ToString());
  120. orderInfo.Completed = bool.Parse(orderRow["Completed"].ToString());
  121. orderInfo.Canceled = bool.Parse(orderRow["Canceled"].ToString());
  122. orderInfo.Comments = orderRow["Comments"].ToString();
  123. orderInfo.CustomerName = orderRow["CustomerName"].ToString();
  124. orderInfo.ShippingAddress = orderRow["ShippingAddress"].ToString();
  125. orderInfo.CustomerEmail = orderRow["CustomerEmail"].ToString();
  126. // return the OrderInfo object
  127. return orderInfo;
  128. }
  129. // Retrieve the order details (the products that are part of that order)
  130. public static DataTable GetDetails(string orderID)
  131. {
  132. // get a configured DbCommand object
  133. DbCommand comm = GenericDataAccess.CreateCommand();
  134. // set the stored procedure name
  135. comm.CommandText = "OrderGetDetails";
  136. // create a new parameter
  137. DbParameter param = comm.CreateParameter();
  138. param.ParameterName = "@OrderID";
  139. param.Value = orderID;
  140. param.DbType = DbType.Int32;
  141. comm.Parameters.Add(param);
  142. // return the results
  143. DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
  144. return table;
  145. }
  146. // Update an order
  147. public static void Update(OrderInfo orderInfo)
  148. {
  149. // get a configured DbCommand object
  150. DbCommand comm = GenericDataAccess.CreateCommand();
  151. // set the stored procedure name
  152. comm.CommandText = "OrderUpdate";
  153. // create a new parameter
  154. DbParameter param = comm.CreateParameter();
  155. param.ParameterName = "@OrderID";
  156. param.Value = orderInfo.OrderID;
  157. param.DbType = DbType.Int32;
  158. comm.Parameters.Add(param);
  159. // create a new parameter
  160. param = comm.CreateParameter();
  161. param.ParameterName = "@DateCreated";
  162. param.Value = orderInfo.DateCreated;
  163. param.DbType = DbType.DateTime;
  164. comm.Parameters.Add(param);
  165. // The DateShipped parameter is sent only if data is available
  166. if (orderInfo.DateShipped.Trim() != "")
  167. {
  168. param = comm.CreateParameter();
  169. param.ParameterName = "@DateShipped";
  170. param.Value = orderInfo.DateShipped;
  171. param.DbType = DbType.DateTime;
  172. comm.Parameters.Add(param);
  173. }
  174. // create a new parameter
  175. param = comm.CreateParameter();
  176. param.ParameterName = "@Verified";
  177. param.Value = orderInfo.Verified;
  178. param.DbType = DbType.Byte;
  179. comm.Parameters.Add(param);
  180. // create a new parameter
  181. param = comm.CreateParameter();
  182. param.ParameterName = "@Completed";
  183. param.Value = orderInfo.Completed;
  184. param.DbType = DbType.Byte;
  185. comm.Parameters.Add(param);
  186. // create a new parameter
  187. param = comm.CreateParameter();
  188. param.ParameterName = "@Canceled";
  189. param.Value = orderInfo.Canceled;
  190. param.DbType = DbType.Byte;
  191. comm.Parameters.Add(param);
  192. // create a new parameter
  193. param = comm.CreateParameter();
  194. param.ParameterName = "@Comments";
  195. param.Value = orderInfo.Comments;
  196. param.DbType = DbType.String;
  197. comm.Parameters.Add(param);
  198. // create a new parameter
  199. param = comm.CreateParameter();
  200. param.ParameterName = "@CustomerName";
  201. param.Value = orderInfo.CustomerName;
  202. param.DbType = DbType.String;
  203. comm.Parameters.Add(param);
  204. // create a new parameter
  205. param = comm.CreateParameter();
  206. param.ParameterName = "@ShippingAddress";
  207. param.Value = orderInfo.ShippingAddress;
  208. param.DbType = DbType.String;
  209. comm.Parameters.Add(param);
  210. // create a new parameter
  211. param = comm.CreateParameter();
  212. param.ParameterName = "@CustomerEmail";
  213. param.Value = orderInfo.CustomerEmail;
  214. param.DbType = DbType.String;
  215. comm.Parameters.Add(param);
  216. // return the results
  217. GenericDataAccess.ExecuteNonQuery(comm);
  218. }
  219. // Mark an order as verified
  220. public static void MarkVerified(string orderId)
  221. {
  222. // get a configured DbCommand object
  223. DbCommand comm = GenericDataAccess.CreateCommand();
  224. // set the stored procedure name
  225. comm.CommandText = "OrderMarkVerified";
  226. // create a new parameter
  227. DbParameter param = comm.CreateParameter();
  228. param.ParameterName = "@OrderID";
  229. param.Value = orderId;
  230. param.DbType = DbType.Int32;
  231. comm.Parameters.Add(param);
  232. // return the results
  233. GenericDataAccess.ExecuteNonQuery(comm);
  234. }
  235. // Mark an order as completed
  236. public static void MarkCompleted(string orderId)
  237. {
  238. // get a configured DbCommand object
  239. DbCommand comm = GenericDataAccess.CreateCommand();
  240. // set the stored procedure name
  241. comm.CommandText = "OrderMarkCompleted";
  242. // create a new parameter
  243. DbParameter param = comm.CreateParameter();
  244. param.ParameterName = "@OrderID";
  245. param.Value = orderId;
  246. param.DbType = DbType.Int32;
  247. comm.Parameters.Add(param);
  248. // return the results
  249. GenericDataAccess.ExecuteNonQuery(comm);
  250. }
  251. // Mark an order as canceled
  252. public static void MarkCanceled(string orderId)
  253. {
  254. // get a configured DbCommand object
  255. DbCommand comm = GenericDataAccess.CreateCommand();
  256. // set the stored procedure name
  257. comm.CommandText = "OrderMarkCanceled";
  258. // create a new parameter
  259. DbParameter param = comm.CreateParameter();
  260. param.ParameterName = "@OrderID";
  261. param.Value = orderId;
  262. param.DbType = DbType.Int32;
  263. comm.Parameters.Add(param);
  264. // return the results
  265. GenericDataAccess.ExecuteNonQuery(comm);
  266. }
  267. }