2014-02-09 43 views
1

我有兩個表叫付款和約會關係彼此使用約會。對於我目前的代碼,它只會將值插入付款表。我想實現的是,如果我輸入包含委任名稱的表單中的值,然後單擊提交,那麼我剛輸入的任務也會更新該預約表aStatus的記錄,以完成或等待。對於我的aStatus組合框,我用等待填充它並在項目屬性中完成。目前我的代碼只能插入付款表中。 aStatus在另一張桌子是預約表。如何將值插入到表中並使用一個按鈕更新另一個表中的值?

這是我爲aStatus插入dropdownlist代碼。我想要它更新預約表思想的一個狀態。那麼如何將此代碼與我的底部代碼結合在一個按鈕中?此代碼將更新預約表中的狀態,底部的代碼將在支付表中插入值。

string value = cbaStatus.SelectedItem == null ? "waiting" : cbaStatus.SelectedItem.ToString(); 
updateCmd.Parameters.AddWithValue("@cbaStatus", value); 

我的形式

enter image description here

我的表和關係

enter image description here

以下錯誤史蒂夫代碼

enter image description here

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; 
} 

回答

1

你有兩個選擇,通過參數的存儲過程中插入支付表中的記錄並更新Appointement表,或從你的代碼執行兩個命令。

無論採用哪種方式,您都需要提供交易以避免存儲付款記錄(如有錯誤)並且無法更新相應的預約記錄。

讓我們嘗試的代碼版本(請測試它,因爲我在這裏寫上飛)

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 [email protected] 
          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; 
     } 
    } 
} 
+0

我得到錯誤(第3圖像)@Steve – Pony

+3

@Jordjmax在某些時候,你必須採取所有權和了解您爲學校項目轉入的代碼。 –

+0

opps該約會有一個拼寫錯誤,我認爲。好吧,哈哈。 @凱斯佩恩 – Pony

相關問題