Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. string value = cbaStatus.SelectedItem == null ? "waiting" : cbaStatus.SelectedItem.ToString();
  2. updateCmd.Parameters.AddWithValue("@cbaStatus", value);
  3.  
  4. private void btnSubmit_Click(object sender, EventArgs e)
  5. {
  6. int result = AddPaymentRecord();
  7. if (result > 0)
  8. {
  9. MessageBox.Show("Insert Successful");
  10. txtamount.Clear();
  11. txtamountPaid.Clear();
  12. txtappointmentID.Clear();
  13. txtamount.Focus();
  14. }
  15. else
  16. {
  17. MessageBox.Show("Insert Fail");
  18. txtamount.Clear();
  19. txtamountPaid.Clear();
  20. txtappointmentID.Clear();
  21. txtamount.Focus();
  22. }
  23. }
  24.  
  25. private int AddPaymentRecord()
  26. {
  27. int result = 0;
  28.  
  29. string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
  30.  
  31. SqlConnection myConnect = new SqlConnection(strConnectionString);
  32.  
  33. String strCommandText = "INSERT PAYMENT(amount, amountPaid, paymentDate, paymentType, appointmentID) "
  34. + " VALUES (@Newamount, @NewamountPaid,@NewpaymentDate, @NewpaymentType, @NewappointmentID)";
  35.  
  36. SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);
  37. updateCmd.Parameters.AddWithValue("@Newamount", txtamount.Text);
  38. updateCmd.Parameters.AddWithValue("@NewamountPaid", txtamountPaid.Text);
  39. updateCmd.Parameters.AddWithValue("@NewpaymentDate", dtppaymentDate.Value);
  40. if (rbCash.Checked)
  41. updateCmd.Parameters.AddWithValue("@NewpaymentType", "Cash");
  42. else
  43. updateCmd.Parameters.AddWithValue("@NewpaymentType", "Credit Card");
  44. updateCmd.Parameters.AddWithValue("@NewappointmentID", txtappointmentID.Text);
  45.  
  46. myConnect.Open();
  47.  
  48. result = updateCmd.ExecuteNonQuery();
  49.  
  50. myConnect.Close();
  51. return result;
  52. }
  53.  
  54. private int AddPaymentRecord()
  55. {
  56. int result = 0;
  57.  
  58. // The command text contains two statements separated by a semicolon
  59. String strCommandText = @"INSERT PAYMENT(amount, amountPaid, paymentDate,
  60. paymentType, appointmentID) VALUES (@Newamount,
  61. @NewamountPaid,@NewpaymentDate,@NewpaymentType,
  62. @NewappointmentID);
  63. UPDATE Appointment SET aStatus=@cbaStatus
  64. WHERE appointmentID = @NewappointmentID";
  65.  
  66. string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
  67.  
  68. using(SqlConnection myConnect = new SqlConnection(strConnectionString))
  69. {
  70. myConnect.Open();
  71.  
  72. // Start a transaction to be sure that the two commands are both executed
  73. SqlTransaction tran = myConnect.BeginTransaction();
  74. try
  75. {
  76. using(SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect, tran))
  77. {
  78. updateCmd.Parameters.AddWithValue("@Newamount", txtamount.Text);
  79. updateCmd.Parameters.AddWithValue("@NewamountPaid", txtamountPaid.Text);
  80. updateCmd.Parameters.AddWithValue("@NewpaymentDate", dtppaymentDate.Value);
  81. if (rbCash.Checked)
  82. updateCmd.Parameters.AddWithValue("@NewpaymentType", "Cash");
  83. else
  84. updateCmd.Parameters.AddWithValue("@NewpaymentType", "Credit Card");
  85. updateCmd.Parameters.AddWithValue("@NewappointmentID", txtappointmentID.Text);
  86. string value = cbaStatus.SelectedItem == null ?
  87. "waiting" : cbaStatus.SelectedItem.ToString();
  88.  
  89. // Add also the parameter required by the second batch statement
  90. updateCmd.Parameters.AddWithValue("@cbaStatus", value);
  91. result = updateCmd.ExecuteNonQuery();
  92.  
  93. // If we reach this point we have updated both records.
  94. // Commit the changes
  95. tran.Commit();
  96. }
  97. return result;
  98. }
  99. catch
  100. {
  101. // Something wrong. rollback any changes and rethrow the exception
  102. // let the caller code handle this exception.
  103. tran.Rollback();
  104. throw;
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement