数据未以正确的方式保存到数据库中

时间:2012-12-13 07:45:44

标签: c#

private void SaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            if (CashPaymentGridView.Rows.Count > 1)
            {
                CashPaymentandReceivedE cashpament =new CashPaymentandReceivedE();
                List<CashPaymentandReceivedE> cashpaymentList = new List<CashPaymentandReceivedE>();
                foreach ( DataGridViewRow rows in CashPaymentGridView.Rows)
                {
                    if (rows.IsNewRow )
                    {
                        break ;
                    }
                    cashpament.VR_NO= Convert.ToInt16(VoucherNoTextBox.Text);
                    cashpament.VR_DATE  = VrDate.Value ;
                    cashpament.ETYPE = "CPV";
                    cashpament.USER_ID = "1";
                    cashpament.PARTY_ID= Convert.ToString (rows.Cells[2].Value)  ;
                    cashpament.DESCRIPTION  = Convert.ToString ( rows.Cells[3].Value);
                    cashpament.INVOICE = Convert.ToString(rows.Cells[4].Value);
                    cashpament.DEBIT  = Convert.ToInt32(rows.Cells[5].Value);
                    cashpament.CREDIT = 0;
                    cashpaymentList.Add(cashpament);

                    cashpament = new CashPaymentandReceivedE();
                    cashpament.VR_NO =Convert.ToInt16(VoucherNoTextBox.Text);
                    cashpament.VR_DATE = VrDate.Value;
                    cashpament.ETYPE = "CPV";
                    cashpament.USER_ID = "1";
                    cashpament.PARTY_ID = NewAccountsDAL.Get_Id_Name ("CASH");
                    cashpament.DESCRIPTION = Convert.ToString(rows.Cells[3].Value);
                    cashpament.INVOICE = Convert.ToString(rows.Cells[4].Value);
                    cashpament.CREDIT = Convert.ToInt32(rows.Cells[5].Value);
                    cashpament.DEBIT = 0;
                    cashpaymentList.Add(cashpament);
                }
                if (CashPaymentandReceivedDAL.Save(cashpaymentList))
                {
                    MessageBox.Show("SAVE SUCCESSFULLY...............");
                    ResetForm();
                }
            }
            else
            {
                MessageBox.Show ("Please select atleast one record.....");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message );
        }
    }

保存数据的存储过程如下。

public static bool Save(List  <CashPaymentandReceivedE> cashreceivedpayment)
    {
        bool blnResult = false;
        SqlConnection objSqlConnection = new SqlConnection(ConnectionString.Connection);
        //SqlTransaction objSqlTransaction = null;
        try
        {
            objSqlConnection.Open();
            //objSqlTransaction = objSqlConnection.BeginTransaction();
            int R = 0;
            while (R < cashreceivedpayment.Count )
            {

                SqlCommand objSqlCommand = new SqlCommand("CASHRECEIVED_Save", objSqlConnection);
                objSqlCommand.CommandType = CommandType.StoredProcedure;

                //SqlParameter objIdentityParameter = objSqlCommand.Parameters.Add("@PLED_ID", SqlDbType.BigInt);
                //objIdentityParameter.Direction = ParameterDirection.Output;

                //objSqlCommand.Parameters.AddWithValue("@PLED_ID", cashreceivedpayment[R].PLED_ID);
                objSqlCommand.Parameters.AddWithValue("@COMPANY_ID", "1");
                objSqlCommand.Parameters.AddWithValue("@PARTY_ID", cashreceivedpayment[R].PARTY_ID);
                objSqlCommand.Parameters.AddWithValue("@VR_NO", cashreceivedpayment[R].VR_NO);
                objSqlCommand.Parameters.AddWithValue("@ETYPE", cashreceivedpayment[R].ETYPE);
                objSqlCommand.Parameters.AddWithValue("@VR_DATE", cashreceivedpayment[R].VR_DATE);
                objSqlCommand.Parameters.AddWithValue("@DESCRIPTION", cashreceivedpayment[R].DESCRIPTION);
                objSqlCommand.Parameters.AddWithValue("@DEBIT", cashreceivedpayment[R].DEBIT);
                objSqlCommand.Parameters.AddWithValue("@CREDIT", cashreceivedpayment[R].CREDIT);
                objSqlCommand.Parameters.AddWithValue("@USER_ID", cashreceivedpayment[R].USER_ID);
                //objSqlCommand.Parameters.AddWithValue("@COMPNAY_ID", cashreceivedpayment[R].COMPANY_ID);
                objSqlCommand.Parameters.AddWithValue("@DESCRIPTION2", "DESCRIPTION2");
                objSqlCommand.Parameters.AddWithValue("@INVOICE", cashreceivedpayment[R].INVOICE);
                objSqlCommand.ExecuteNonQuery();
                R++;
                blnResult = true;
            }

        }
        catch (Exception ex)
        {
            //objSqlTransaction.Rollback();
            MessageBox.Show(ex.Message);
        }
        finally
        {
            //objSqlTransaction.Commit();
            objSqlConnection.Close();

        }
        return blnResult;
    }

当我保存记录时,一条记录应该是Party_id,一条记录应该是现金。 但是当我选择一个以上的记录时,只有一个条目可以节省现金。当我加载记录时,一条记录被加载。如果你理解的话,请帮助.......

1 个答案:

答案 0 :(得分:0)

您正在创建一个 CashPaymentandReceivedE对象,并在列表的每次迭代中添加对它的引用。然后,您还要在每次迭代时更改单个对象中的所有数据。只需移动这一行:

CashPaymentandReceivedE cashpament =new CashPaymentandReceivedE();

... 内部foreach语句中,问题应该得到解决。

在此之前,请确保您了解为什么您的代码的行为是这样的。理解列表不包含对象非常重要 - 它包含对象的引用。在您的情况下,它将包含对单个对象的多个引用,直到您修复它。

我还强烈建议您在foreach方法中使用Save - 如果确实因某种原因需要索引,请使用for循环而不是while