You have two options, pass the parameters to a Stored Procedure that insert the record in the payment table and updates the Appointement table, or execute the two commands from your code.
Either way you need to provide a Transaction to avoid storing the payment record (in case of errors) and fail to update the appropriate appointment record.
Let's try the code version (please test it because I have written here on the fly)
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;
}
}
}