Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data; //Need this for the DataSet, DataRow classes
  6. using System.Data.SqlClient; //Need this for the SqlCommand, SqlConnection classes
  7. using System.Configuration; //Need this to read from Web.Config file
  8.  
  9. namespace OnlinePetStoreManagementSystem.ProductManagement.Domain
  10. {
  11. public class CategoryManager
  12. {
  13. /*------------------------------------------------------------------
  14. Method Name: GetOneCategroy
  15. Parameters: inCategoryId
  16. Purpose: Accept the course id from the calling program, to generate
  17. a proper SQL to query for one category record from the database.
  18. Returns: Returns [one] Course object only. And this Course object
  19. is filled with the
  20. course record obtained from the database
  21. -------------------------------------------------------------------*/
  22.  
  23. //Create an empty DataSet object, ds.
  24. public Category GetOneCategory(int inCategoryId)
  25. {
  26. //This object has the capability to hold information
  27. DataSet ds = new DataSet();
  28. //Create an object category object, course
  29. Category category = new Category();
  30. using (SqlConnection cn = new SqlConnection())
  31. {
  32. using (SqlCommand cmd = new SqlCommand())
  33. {
  34. using (SqlDataAdapter da = new SqlDataAdapter())
  35. {
  36. //Obtain connection string information from web.config
  37. cn.ConnectionString =
  38. ConfigurationManager.ConnectionStrings["WebaConnectionString"].ToString();
  39. cmd.Connection = cn;//tell the cmd to use the cn
  40. //Suppy the cmd with the necessary SQL
  41. //The SELECT statement has a blank, @inCourseId.
  42. //The cmd.Parameters.Add(...) is used to inform the SqlCommand object,
  43. //about this blank. And, at the same time, a value is supplied to fill up the
  44. //blank.
  45. cmd.CommandText = "SELECT * FROM Category WHERE CategoryId = @inCategoryId";
  46. cmd.Parameters.Add("@inCategoryId", SqlDbType.Int).Value = inCategoryId;
  47. //Tell the da (DataAdapter) to use the cmd
  48. da.SelectCommand = cmd;
  49. //Open an active connection
  50. cn.Open();
  51. //Tell the da, so that the da can tell the cmd to send the SQL
  52. //to the database. Database receives the SQL and returns one course record.
  53. //The SqlDataAdapter object, da will capture the category record and write them
  54. //into the Dataset(ds). The results are grouped and given a name,
  55. //CategoryData. (the named results 'CategoryData' is also
  56. //known as a DataTable)
  57. da.Fill(ds, "CategoryData");
  58. //Note that, there should only be one row of category info
  59. //sitting inside the CategoryData DataTable.
  60. cn.Close();//close the connection
  61.  
  62. }
  63. }
  64. }
  65. //There is only one datarow in the DataTable, ds.Tables["CategoryData"]
  66. //Use [0] to that datarow and copy out the category data into the category object.
  67. DataRow dr = ds.Tables["CategoryData"].Rows[0];
  68. category.CategoryId = int.Parse(dr["CategoryId"].ToString());
  69. category.CategoryName = dr["CategoryName"].ToString();
  70. category.CreatedAt = DateTime.Parse(dr["CreatedAt"].ToString());
  71. category.CreatedBy = dr["CreatedBy"].ToString();
  72. category.UpdatedAt = DateTime.Parse(dr["UpdatedAt"].ToString());
  73. category.UpdatedBy = dr["UpdatedBy"].ToString();
  74.  
  75. return category;//return the List to the calling program.
  76. }//end of GetOneCategory() method
  77.  
  78. public List<Category> GetAllCategory()
  79. {
  80. /*------------------------------------------------------------------
  81. Method Name: GetAllCategory
  82. Parameters: No parameters defined.
  83. Purpose: Generates a proper SQL to query for all course record from the database
  84. which are not marked as deleted.
  85. Returns: Returns a List of Category objects which is filled with the
  86. course records obtained from the database. For example, if there
  87. are 4 category records obtained from the database, there should be
  88. a List of 4 Category instances. Each instance contain one course information.
  89. -------------------------------------------------------------------*/
  90. //Create an empty DataSet object, ds.
  91. //This object has the capability to hold information
  92. DataSet ds = new DataSet();
  93. //Create an empty List, categoryList
  94. List<Category> categoryList = new List<Category>();
  95. using (SqlConnection cn = new SqlConnection())
  96. {
  97. using (SqlCommand cmd = new SqlCommand())
  98. {
  99. using (SqlDataAdapter da = new SqlDataAdapter())
  100. {
  101. //Obtain connection string information from web.config
  102. cn.ConnectionString =
  103. ConfigurationManager.ConnectionStrings["WebaConnectionString"].ToString();
  104. cmd.Connection = cn;//tell the cmd to use the cn
  105. //Suppy the cmd with the necessary SQL
  106. cmd.CommandText = "SELECT * FROM Category WHERE DeletedAt IS NULL";
  107. //Tell the da (DataAdapter) to use the cmd
  108. da.SelectCommand = cmd;
  109. //Open an active connection
  110. cn.Open();
  111. //Tell the da, so that the da can tell the cmd to send the SQL
  112. //to the database. Database receives the SQL and returns the results.
  113. //The SqlDataAdapter object, da will capture the result and write them
  114. //into the Dataset(ds). The results are grouped and given a name,
  115. //CourseData. (the named results 'CourseData' is also
  116. //known as a DataTable)
  117. da.Fill(ds, "CategoryData");
  118. cn.Close();//close the connection
  119.  
  120. }
  121. }
  122. }
  123. //loop through the datarows in the DataTable, ds.Tables["CourseData"]
  124. //to fetch all the course records and pump them into the List, courseList.
  125. foreach (DataRow dr in ds.Tables["CategoryData"].Rows)
  126. {
  127. Category category = new Category();
  128. category.CategoryId = int.Parse(dr["CategoryId"].ToString());
  129. category.CategoryName = dr["CategoryName"].ToString();
  130. category.CreatedAt = DateTime.Parse(dr["CreatedAt"].ToString());
  131. category.CreatedBy = dr["CreatedBy"].ToString();
  132. category.UpdatedAt = DateTime.Parse(dr["UpdatedAt"].ToString());
  133. category.UpdatedBy = dr["UpdatedBy"].ToString();
  134. categoryList.Add(category);
  135. }
  136. return categoryList;//return the List to the calling program.
  137. }//End of GetAllCourse() method
  138.  
  139.  
  140. //Add Category Method
  141. public int AddCategory(string inCategoryName, string inCreatedBy, string inUpdatedBy)
  142. {
  143. int newCategoryId = 0;
  144. using (SqlConnection cn = new SqlConnection())
  145. {
  146. cn.ConnectionString = ConfigurationManager.ConnectionStrings["WebaConnectionString"].ToString();
  147. using (SqlCommand cmd = new SqlCommand())
  148. {
  149. cmd.Connection = cn;//tell the cmd to use the cn
  150. cmd.CommandText = "INSERT Category (CategoryName,CreatedBy,UpdatedBy ) " +
  151. " OUTPUT Inserted.CategoryId VALUES " +
  152. "(@inCategoryName,@inCreatedBy,@inUpdatedBy)";
  153. cmd.Parameters.Add("@inCategoryName", SqlDbType.VarChar, 100).Value = inCategoryName;
  154. cmd.Parameters.Add("@inCreatedBy", SqlDbType.VarChar, 30).Value = inCreatedBy;
  155. cmd.Parameters.Add("@inUpdatedBy", SqlDbType.VarChar, 30).Value = inUpdatedBy;
  156.  
  157. cn.Open();
  158. try
  159. {
  160. newCategoryId = Int32.Parse(cmd.ExecuteScalar().ToString());
  161. }
  162. catch (SqlException sqlEx)
  163. { //Capture unique constraint violation
  164. if (sqlEx.Message.Contains("uc_Category_CategoryName") == true)
  165. {
  166. string messageTemplate = "Unable to save due to {0} category id found in other records.";
  167. string message = string.Format(messageTemplate, inCategoryName);
  168. //Throw an exception message to the calling program.
  169. throw new System.ArgumentException(message); //The method will end processing here.
  170. }
  171. else
  172. {
  173. throw new System.ArgumentException(sqlEx.Message);
  174. }
  175. }
  176. cn.Close();
  177. }//end of using SqlCommand, cmd
  178. }//end of using SqlConnection, cn
  179. //If there are no runtime error (or exceptions) occurred, the
  180. //.NET engine will execute the following before finish executing
  181. //this AddOneStudent() method.
  182. return newCategoryId; //return the new record id back to the calling program.
  183. }//End of AddOneStudent() method
  184.  
  185.  
  186. public int CheckNumberOfCategoryRecords(int inCategoryId)
  187. {
  188. /*------------------------------------------------------------------
  189. Method Name: CheckNumberOfStudentRecords
  190. Parameters: inCourseId
  191. Purpose: Accept the course id from the calling program, to generate
  192. a proper SELECT COUNT(*) SQL to find out the number of student records
  193. linked to a specific course (by checking the course id).
  194. This method is usually called to display information, or to check for information
  195. before marking a Course record as deleted.
  196. Returns: Returns int number of student records to the calling program.
  197. -------------------------------------------------------------------*/
  198. int numOfCategoryRecords = 0;
  199.  
  200. using (SqlConnection cn = new SqlConnection())
  201. {
  202. using (SqlCommand cmd = new SqlCommand())
  203. {
  204.  
  205. //Obtain connection string information from web.config
  206. cn.ConnectionString =
  207. ConfigurationManager.ConnectionStrings["WebaConnectionString"].ToString();
  208. cmd.Connection = cn;//tell the cmd to use the cn
  209. //Suppy the cmd with the necessary SQL
  210. //The SELECT statement has a blank, @inCourseId.
  211. //The cmd.Parameters.Add(...) is used to inform the SqlCommand object,
  212. //about this blank. And, at the same time, a value is supplied to fill up the
  213. //blank.
  214. cmd.CommandText = "SELECT COUNT(CategorytId) FROM Category WHERE CategoryId = @inCategoryId";
  215. cmd.Parameters.Add("@inCategoryId", SqlDbType.Int).Value = inCategoryId;
  216.  
  217. //Open an active connection
  218. cn.Open();
  219. //Send the SQL to the database server. If the UPDATE operation
  220. //is successful, the numOfRecordsAffectd will have a value of 1.
  221. //If the UPDATE process is not successful, the numOfRecordsAffected will
  222. //have a value of 0.
  223. numOfCategoryRecords = int.Parse(cmd.ExecuteScalar().ToString());
  224. //sitting inside the CourseData DataTable.
  225. cn.Close();//close the connection
  226.  
  227.  
  228. }//end of using(SqlCommand cmd ...)
  229. }//end of using(SqlConnection cn ...)
  230.  
  231. return numOfCategoryRecords;
  232. }//end of CheckNumberOfStudentRecords() method
  233.  
  234.  
  235. /*------------------------------------------------------------------
  236. Method Name: UpdateOneCourse
  237. Parameters: inCourseId, inCourseAbbreviation, inCourseName
  238. Purpose: Accepts course information from the calling program, to
  239. prepare an UPDATE course SQL, to update one course record in the
  240. database.
  241. Returns: Returns a Boolean value of either true or false.
  242. If the update process is successful, true is returned.
  243. If the update process is not successful, false is returned.
  244. -------------------------------------------------------------------*/
  245. public bool UpdateOneCategory(int inCategoryId, string inCategoryName, string inCreatedBy, string inUpdatedBy)
  246. {
  247. //Create an Int variable, numOfRecordsAffected
  248. int numOfRecordsAffected = 0;
  249. //Create an empty DataSet object, ds.
  250. //This object has the capability to hold information
  251. DataSet ds = new DataSet();
  252. //Create an object Course object, course
  253. Category category = new Category();
  254. using (SqlConnection cn = new SqlConnection())
  255. {
  256. using (SqlCommand cmd = new SqlCommand())
  257. {
  258. //Obtain connection string information from web.config
  259. cn.ConnectionString =
  260. ConfigurationManager.ConnectionStrings["WebaConnectionString"].ToString();
  261. cmd.Connection = cn;//tell the cmd to use the cn
  262. //Suppy the cmd with the necessary SQL
  263. //The SELECT statement has a blank, @inCourseId.
  264. //The cmd.Parameters.Add(...) is used to inform the SqlCommand object,
  265. //about this blank. And, at the same time, a value is supplied to fill up the
  266. //blank.
  267. cmd.CommandText = "UPDATE Category SET CategoryName = @inCategoryName ";
  268. cmd.CommandText += ", CreatedBy = @inCreatedBy , CreatedAt = GETDATE() ";
  269. cmd.CommandText += ", UpdatedBy = @inUpdatedBy , UpdatedAt = GETDATE() WHERE ";
  270. cmd.CommandText += " CategoryId = @inCategoryId";
  271. cmd.Parameters.Add("@inCategoryId", SqlDbType.Int).Value = inCategoryId;
  272. cmd.Parameters.Add("@inCategoryName", SqlDbType.VarChar, 100).Value = inCategoryName;
  273. cmd.Parameters.Add("@inCreatedBy", SqlDbType.VarChar, 30).Value = inCreatedBy;
  274. cmd.Parameters.Add("@inUpdatedBy", SqlDbType.VarChar, 30).Value = inUpdatedBy;
  275. cn.Open();
  276. try
  277. {
  278. numOfRecordsAffected = cmd.ExecuteNonQuery();
  279. }
  280. catch (SqlException sqlEx)
  281. {
  282. if(sqlEx.Message.Contains("uc_Category_CategoryName") == true)
  283. {
  284. string messageTemplate = "Unable to save due to {0} category id found in other records.";
  285. string message = string.Format(messageTemplate, inCategoryName);
  286. //Throw an exception message to the calling program.
  287. throw new System.ArgumentException(message); //The method will end processing here.
  288. }
  289. }
  290.  
  291. }//end of using (SqlCommand cmd = new SqlCommand())
  292. }//end of using(SqlConnection cn = new SqlConnection())
  293.  
  294. if (numOfRecordsAffected == 1)
  295. {
  296. return true;
  297. }
  298. else
  299. {
  300. return false;
  301. }
  302. }//end of UpdateOneCategroy() method
  303.  
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement