Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. public bool InsertOrUpdateListPriceDetails(List<ListPriceDetail> listPriceDetails)
  2. {
  3. try
  4. {
  5. for (int i = 0; i < listPriceDetails.Count(); i++)
  6. {
  7. var listPriceDetail = listPriceDetails[i];
  8.  
  9. int? ID = GetListPriceDetailID(listPriceDetail);
  10. if (ID.HasValue)
  11. {
  12. listPriceDetail.ID = ID.Value;
  13. if (!UpdateListPriceDetail(listPriceDetail)) return false;
  14. }
  15. else
  16. {
  17. ID = InsertListPriceDetail(listPriceDetail);
  18. if (!ID.HasValue) return false;
  19.  
  20. listPriceDetail.ID = ID.Value;
  21. }
  22. }
  23. }
  24. catch (Exception e)
  25. {
  26. Debug.WriteLine(e.Message);
  27. Transaction.Rollback();
  28. return false;
  29. }
  30.  
  31. return true;
  32. }
  33.  
  34. public bool UpdateListPriceDetail(ListPriceDetail listPriceDetail)
  35. {
  36. try
  37. {
  38. //Se nessun campo è stato modificato l'update non ha senso farlo e quindi l'operazione si conclude con successo
  39. if (!listPriceDetail.CanBeUpdated()) return true;
  40.  
  41. command.Parameters.Clear();
  42. string query = "UPDATE [List_Price_Details] SET ";
  43.  
  44. if (listPriceDetail.MinimumStay.HasValue)
  45. {
  46. query += "[Minimum Stay] = @minimumStay,";
  47. command.Parameters.AddWithValue("@minimumStay", listPriceDetail.MinimumStay);
  48. }
  49. if (listPriceDetail.Quantity.HasValue)
  50. {
  51. query += "[Quantity] = @quantity,";
  52. command.Parameters.AddWithValue("@quantity", listPriceDetail.Quantity);
  53. }
  54. if (listPriceDetail.Release.HasValue)
  55. {
  56. query += "[Release] = @release,";
  57. command.Parameters.AddWithValue("@release", listPriceDetail.Release);
  58. }
  59. if (listPriceDetail.UnitCost.HasValue)
  60. {
  61. query += "[Unit Cost] = @unitCost,";
  62. command.Parameters.AddWithValue("@unitCost", listPriceDetail.UnitCost);
  63. }
  64. if (listPriceDetail.BedAndBreakfast.HasValue)
  65. {
  66. query += "[Bed and Breakfast] = @beb,";
  67. command.Parameters.AddWithValue("@beb", listPriceDetail.BedAndBreakfast);
  68. }
  69. if (listPriceDetail.HalfBoard.HasValue)
  70. {
  71. query += "[Half Board] = @halfBoard,";
  72. command.Parameters.AddWithValue("@halfBoard", listPriceDetail.HalfBoard);
  73. }
  74. if (listPriceDetail.FullBoard.HasValue)
  75. {
  76. query += "[Full Board] = @fullBoard,";
  77. command.Parameters.AddWithValue("@fullBoard", listPriceDetail.FullBoard);
  78. }
  79. if (listPriceDetail.AllInclusive.HasValue)
  80. {
  81. query += "[All Inclusive] = @allInclusive,";
  82. command.Parameters.AddWithValue("@allInclusive", listPriceDetail.AllInclusive);
  83. }
  84. if (listPriceDetail.CheckIn.HasValue)
  85. {
  86. query += "[Check-In] = @checkIn,";
  87. command.Parameters.AddWithValue("@checkIn", listPriceDetail.CheckIn);
  88. }
  89. if (listPriceDetail.CheckOut.HasValue)
  90. {
  91. query += "[Check-Out] = @checkOut,";
  92. command.Parameters.AddWithValue("@checkOut", listPriceDetail.CheckOut);
  93. }
  94. if (!string.IsNullOrEmpty(listPriceDetail.PLStop))
  95. {
  96. query += "[PL Stop] = @plStop,";
  97. command.Parameters.AddWithValue("@plStop", listPriceDetail.PLStop);
  98. }
  99. if (listPriceDetail.PackageID.HasValue)
  100. {
  101. query += "[Package ID] = @packageID,";
  102. command.Parameters.AddWithValue("@packageID", listPriceDetail.PackageID);
  103. }
  104.  
  105. query = query.Remove(query.Length - 1);
  106. query += " WHERE [ID] = @id";
  107. command.Parameters.AddWithValue("@id", listPriceDetail.ID);
  108.  
  109. command.CommandText = query;
  110.  
  111. int row = command.ExecuteNonQuery();
  112.  
  113. //Restituisce true solo se 1 riga viene modificata
  114. if (row != 1)
  115. {
  116. Transaction.Rollback();
  117. return false;
  118. }
  119.  
  120. return true;
  121.  
  122. }
  123. catch (Exception)
  124. {
  125. Transaction.Rollback();
  126. return false;
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement