本文介绍了交易问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 让我解释一下我的情况.客户可以在不同的时间段内为所接受的服务支付金额.假设他在三个不同的日期付款.但是,如果付款金额不正确,则需要更正首次付款.然后,第二和第三次到期的金额必须自动更新. objPayDetails是PaymentDetails类的对象,其中定义了更新功能.在编辑时更新付款的函数是objPayDetails.updatePaymentDetailsByPaymentDetailsID(paramCon,paramCmd,paramTran); ViewState ["PreviousPaidAmount"].ToString()是在特定日期要修改的付款金额 下面是更新按钮的代码:受保护的无效btnUpdate_Click(对象发送者,EventArgs e) { SqlTransaction paramTran = null; DAL objDAL =新的DAL(); 使用(SqlConnection paramCon = objDAL.getActiveConnection()) { 尝试 { SqlCommand paramCmd = paramCon.CreateCommand(); paramCon.Open(); paramTran = paramCon.BeginTransaction("SaveServices"); paramCmd.Connection = paramCon; paramCmd.Transaction = paramTran; DateTime dtTemp = Convert.ToDateTime("1/1/1900"); //更新为tblPaymentDetails objPayDetails = new ClassPaymentDetails(paramCon); objPayDetails.PaymentID = Convert.ToInt64(Request.Params ["PaymentID"].ToString()); objPayDetails.PaymentDetailID = Convert.ToInt64(ViewState ["PaymentDetailsID"].ToString()); 如果(string.IsNullOrEmpty(txtPaymentDate.Text)==否) objPayDetails.PaymentDate = Convert.ToDateTime(txtPaymentDate.Text); 别的 objPayDetails.PaymentDate = dtTemp; 如果(string.IsNullOrEmpty(txtPaidAmount.Text)==否) objPayDetails.PaidAmount = Convert.ToDouble(txtPaidAmount.Text); 如果(string.IsNullOrEmpty(txtDueAmount.Text)==否) objPayDetails.DueAmount = Convert.ToDouble(txtDueAmount.Text); int updatePayDetails = objPayDetails.updatePaymentDetailsByPaymentDetailsID(paramCon,paramCmd,paramTran); //如果支付的金额增加或减少,则更改到期金额 如果(Convert.ToDouble(txtPaidAmount.Text)!= Convert.ToDouble(ViewState ["PreviousPaidAmount"].ToString())) { paramCmd = paramCon.CreateCommand(); paramCmd = new SqlCommand(从tblPaymentDetails中选择PaymentDetailsID,其中PaymentID =""+ Convert.ToInt64(Request.Params [" PaymentID].ToString())+" AND PaymentDetailsID>"+ Convert.ToInt64(ViewState [ "PaymentDetailsID"].ToString())+",paramCon); paramCmd.Transaction = paramTran; SqlDataReader r = paramCmd.ExecuteReader(); 而(r.Read()) { double diff = 0.0; objPayDetails.PaymentDetailID = r.GetInt64(0); int updateDueAmount = 0; 如果(Convert.ToDouble(txtPaidAmount.Text)> Convert.ToDouble(ViewState ["PreviousPaidAmount"].ToString())) { diff = Convert.ToDouble(txtPaidAmount.Text)-Convert.ToDouble(ViewState ["PreviousPaidAmount"].ToString()); updateDueAmount = objPayDetails.updateDueAmountByPaymentDetailsID(paramCon,paramCmd,paramTran);//在此处获取错误:System.ArgumentException:已经有与此命令关联的打开的DataReader,必须首先关闭它. } 别的 { diff = Convert.ToDouble(ViewState ["PreviousPaidAmount"].ToString())-Convert.ToDouble(txtPaidAmount.Text); updateDueAmount = objPayDetails.updateDueAmountByPaymentDetailsID(paramCon,paramCmd,paramTran); } } } paramTran.Commit(); ViewState ["TotalDueAmount"] = txtDueAmount.Text; txtPaymentDate.Text ="; txtPaidAmount.Text ="; //txtDueAmount.Text = ViewState ["TotalDueAmount"].ToString(); BindGrid(); lblMessage.Text =成功保存". } 抓住(前例外) { lblMessage.Text =无法更新...请查看您的输入数据!\ n" + ex.Message.ToString(); } 最后 { 尝试 { 如果(paramTran.Connection!= null) { paramTran.Rollback(); 如果(paramCon.State == ConnectionState.Open) paramCon.Close(); } } 抓住(前例外) { lblMessage.Text =无法更新...请查看您的输入数据!" } } } } 解决方案 Let me explain my scenario. A customer can pay the amount in different time period for the services he takes. Suppose he makes payment in three different dates. But, first payment needs to be rectified if paid amount is incorrect. Then the due amount for second and third has to be updated automatically. objPayDetails is the object of class PaymentDetails where update function is defined. Function that update payment while editing is objPayDetails.updatePaymentDetailsByPaymentDetailsID(paramCon, paramCmd, paramTran);ViewState["PreviousPaidAmount"].ToString() is the amount paid at a particular date which is about to be modified Below is the update button code: protected void btnUpdate_Click(object sender, EventArgs e) { SqlTransaction paramTran = null; DAL objDAL = new DAL(); using (SqlConnection paramCon = objDAL.getActiveConnection()) { try { SqlCommand paramCmd = paramCon.CreateCommand(); paramCon.Open(); paramTran = paramCon.BeginTransaction("SaveServices"); paramCmd.Connection = paramCon; paramCmd.Transaction = paramTran; DateTime dtTemp = Convert.ToDateTime("1/1/1900"); //Update into tblPaymentDetails objPayDetails = new ClassPaymentDetails(paramCon); objPayDetails.PaymentID = Convert.ToInt64(Request.Params["PaymentID"].ToString()); objPayDetails.PaymentDetailID = Convert.ToInt64(ViewState["PaymentDetailsID"].ToString()); if (string.IsNullOrEmpty(txtPaymentDate.Text) == false) objPayDetails.PaymentDate = Convert.ToDateTime(txtPaymentDate.Text); else objPayDetails.PaymentDate = dtTemp; if (string.IsNullOrEmpty(txtPaidAmount.Text) == false) objPayDetails.PaidAmount = Convert.ToDouble(txtPaidAmount.Text); if (string.IsNullOrEmpty(txtDueAmount.Text) == false) objPayDetails.DueAmount = Convert.ToDouble(txtDueAmount.Text); int updatePayDetails = objPayDetails.updatePaymentDetailsByPaymentDetailsID(paramCon, paramCmd, paramTran); //Change the due amount if paid amount is increased or decreased if (Convert.ToDouble(txtPaidAmount.Text) != Convert.ToDouble(ViewState["PreviousPaidAmount"].ToString())) { paramCmd = paramCon.CreateCommand(); paramCmd = new SqlCommand("select PaymentDetailsID from tblPaymentDetails where PaymentID=" + Convert.ToInt64(Request.Params["PaymentID"].ToString()) + " AND PaymentDetailsID>" + Convert.ToInt64(ViewState["PaymentDetailsID"].ToString()) + " ", paramCon); paramCmd.Transaction = paramTran; SqlDataReader r = paramCmd.ExecuteReader(); while (r.Read()) { double diff = 0.0; objPayDetails.PaymentDetailID = r.GetInt64(0); int updateDueAmount = 0; if (Convert.ToDouble(txtPaidAmount.Text) > Convert.ToDouble(ViewState["PreviousPaidAmount"].ToString())) { diff = Convert.ToDouble(txtPaidAmount.Text) - Convert.ToDouble(ViewState["PreviousPaidAmount"].ToString()); updateDueAmount = objPayDetails.updateDueAmountByPaymentDetailsID(paramCon, paramCmd, paramTran);//Get Error here: System.ArgumentException: There is already an open DataReader associated with this Command which must be closed first. } else { diff = Convert.ToDouble(ViewState["PreviousPaidAmount"].ToString()) - Convert.ToDouble(txtPaidAmount.Text); updateDueAmount = objPayDetails.updateDueAmountByPaymentDetailsID(paramCon, paramCmd, paramTran); } } } paramTran.Commit(); ViewState["TotalDueAmount"] = txtDueAmount.Text; txtPaymentDate.Text = ""; txtPaidAmount.Text = ""; //txtDueAmount.Text = ViewState["TotalDueAmount"].ToString(); BindGrid(); lblMessage.Text = "Saved Successfully."; } catch (Exception ex) { lblMessage.Text = "Could not update...Please see your input data!\n"+ex.Message.ToString(); } finally { try { if (paramTran.Connection != null) { paramTran.Rollback(); if (paramCon.State == ConnectionState.Open) paramCon.Close(); } } catch (Exception ex) { lblMessage.Text = "Could not update...Please see your input data!"; } } } } 解决方案 这篇关于交易问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 23:11