Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string value = cbaStatus.SelectedItem == null ? "waiting" : cbaStatus.SelectedItem.ToString();
- updateCmd.Parameters.AddWithValue("@cbaStatus", value);
- private void btnSubmit_Click(object sender, EventArgs e)
- {
- int result = AddPaymentRecord();
- if (result > 0)
- {
- MessageBox.Show("Insert Successful");
- txtamount.Clear();
- txtamountPaid.Clear();
- txtappointmentID.Clear();
- txtamount.Focus();
- }
- else
- {
- MessageBox.Show("Insert Fail");
- txtamount.Clear();
- txtamountPaid.Clear();
- txtappointmentID.Clear();
- txtamount.Focus();
- }
- }
- private int AddPaymentRecord()
- {
- int result = 0;
- string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
- SqlConnection myConnect = new SqlConnection(strConnectionString);
- String strCommandText = "INSERT PAYMENT(amount, amountPaid, paymentDate, paymentType, appointmentID) "
- + " VALUES (@Newamount, @NewamountPaid,@NewpaymentDate, @NewpaymentType, @NewappointmentID)";
- SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);
- updateCmd.Parameters.AddWithValue("@Newamount", txtamount.Text);
- updateCmd.Parameters.AddWithValue("@NewamountPaid", txtamountPaid.Text);
- updateCmd.Parameters.AddWithValue("@NewpaymentDate", dtppaymentDate.Value);
- if (rbCash.Checked)
- updateCmd.Parameters.AddWithValue("@NewpaymentType", "Cash");
- else
- updateCmd.Parameters.AddWithValue("@NewpaymentType", "Credit Card");
- updateCmd.Parameters.AddWithValue("@NewappointmentID", txtappointmentID.Text);
- myConnect.Open();
- result = updateCmd.ExecuteNonQuery();
- myConnect.Close();
- return result;
- }
- private int AddPaymentRecord()
- {
- int result = 0;
- // The command text contains two statements separated by a semicolon
- String strCommandText = @"INSERT PAYMENT(amount, amountPaid, paymentDate,
- paymentType, appointmentID) VALUES (@Newamount,
- @NewamountPaid,@NewpaymentDate,@NewpaymentType,
- @NewappointmentID);
- UPDATE Appointment SET aStatus=@cbaStatus
- WHERE appointmentID = @NewappointmentID";
- string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
- using(SqlConnection myConnect = new SqlConnection(strConnectionString))
- {
- myConnect.Open();
- // Start a transaction to be sure that the two commands are both executed
- SqlTransaction tran = myConnect.BeginTransaction();
- try
- {
- using(SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect, tran))
- {
- updateCmd.Parameters.AddWithValue("@Newamount", txtamount.Text);
- updateCmd.Parameters.AddWithValue("@NewamountPaid", txtamountPaid.Text);
- updateCmd.Parameters.AddWithValue("@NewpaymentDate", dtppaymentDate.Value);
- if (rbCash.Checked)
- updateCmd.Parameters.AddWithValue("@NewpaymentType", "Cash");
- else
- updateCmd.Parameters.AddWithValue("@NewpaymentType", "Credit Card");
- updateCmd.Parameters.AddWithValue("@NewappointmentID", txtappointmentID.Text);
- string value = cbaStatus.SelectedItem == null ?
- "waiting" : cbaStatus.SelectedItem.ToString();
- // Add also the parameter required by the second batch statement
- updateCmd.Parameters.AddWithValue("@cbaStatus", value);
- result = updateCmd.ExecuteNonQuery();
- // If we reach this point we have updated both records.
- // Commit the changes
- tran.Commit();
- }
- return result;
- }
- catch
- {
- // Something wrong. rollback any changes and rethrow the exception
- // let the caller code handle this exception.
- tran.Rollback();
- throw;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement