Forum:  LLBLGen Pro Runtime Framework

Thread:  No Connection present. Cannot execute command.


Rob (User)   Posted on: 10-May-2005 11:10:13.
Hello!

I get the error message as described in the subject.

I use transactions and assume that I am running out of connections or something.
It is not like I am adding every object in the world to the transaction so I am assuming that I am not doing it correctly. I wonder if you can have a look and perhaps see what I am doing wrong?

I am quoting two methods below. One creates the transaction (deposit) and fiddle about with a bunch of objects. It then calls the other method (createNewToBeAlloted)

The deposit method receives a DataTable dtDeposits which I thought was going to be able to contain hundreds of rows. However, I get the problem when I am only passing a table containing one row. The dataview dvFundProducts would typically contain up to 20 rows. But in my case there are only 3 rows in it. My problem occurs the second time the createNewToBeAlloted method is called on the row that saves the object. This indicates that the code actually runs, it just seems it uses up all connections or something.

Obvsioulsy if my code is eating connections I need to do it differently, but I do not quite know how...

How do I solve the problem? Grateful for any tips!

deposit
Code:

        public bool deposit(int userID,
            DataTable dtDeposits,
            DataView dvFundProducts,
            DateTime transactiondate,
            string description,
            out string sError,
            bool forValidation
            )
        {
            bool success = false;
            sError = "";
            AccountEntity account = new AccountEntity();

            Transaction dbtxn = new Transaction(IsolationLevel.ReadCommitted, "Custom");

            try
            {
                foreach(DataRow dr in dtDeposits.Rows)
                {
                    if(!validateFundData(this.FundID,
                        DateTime.Now,
                        Convert.ToDecimal(dr["AmountCleared"]),
                        null,
                        out sError))
                    {
                        throw new Exception(sError);
                    }

                    account.FetchUsingPK(Convert.ToInt32(dr["AccountID"]));

                    BizEventEntity bizEvent = new BizEventEntity();
                    if(dr["BizEventID"] != Convert.DBNull)
                    {
                        bizEvent.FetchUsingPK(Convert.ToInt32(dr["BizEventID"]));
                    }

                    PhraseEntity phrase = new PhraseEntity(Constants.Phrase___Deposit_Comment);
                    string sDescription = phrase.Description;
                    phrase = null;
                    if(Convert.ToDecimal(dr["AmountSent"]) != Convert.ToDecimal(dr["AmountCleared"]))
                        sDescription = sDescription.Replace("_SENT_", "(Sent $" + dr["AmountSent"].ToString() + ") - ");
                    else
                        sDescription = sDescription.Replace("_SENT_", "");

                    sDescription = sDescription.Replace("_COMMENT_", description);

                    BizTransactionEntity biztxn = new BizTransactionEntity();
                    biztxn.FromAccountAmount = 0;
                    biztxn.ToAccountAmount = Convert.ToDecimal(dr["AmountCleared"]);
                    biztxn.ToAccountID = account.AccountID;
                    biztxn.ToBeforeBalance = account.Balance;
                    biztxn.ToAfterBalance = account.Balance + Convert.ToDecimal(dr["AmountCleared"]);
                    biztxn.BizTransactionStatusID = Constants.BizTransactionStatus___Complete;
                    biztxn.BizTransactionTypeID = Constants.BizTransactionType___Deposit;
                    biztxn.BizTransactionDateTime = transactiondate;
                    biztxn.Description = sDescription;
                    biztxn.CreatedDate = DateTime.Now;
                    biztxn.CreatedUserID = userID;

                    dbtxn.Add(biztxn);

                    success = biztxn.Save();

                    if(Convert.ToInt32(dr["PaymentTypeID"]) == Constants.PaymentType___Cheque)
                    {
                        //if supplied cheque details are the same as on BizEvent. Use BizEvent cheque
                        if(Convert.ToBoolean(dr["UseExistingCheque"]) ||
                            (bizEvent.Cheques.Count > 0 &&
                            bizEvent.Cheques[0].Drawer == dr["Drawer"].ToString() &&
                            bizEvent.Cheques[0].BankName == dr["BankName"].ToString() &&
                            bizEvent.Cheques[0].ChequeNo == dr["ChequeNo"].ToString() &&
                            bizEvent.Cheques[0].BSB == dr["BSB"].ToString() &&
                            bizEvent.Cheques[0].Account == dr["Account"].ToString()))
                        {
                            bizEvent.Cheques[0].BizTransactionID = biztxn.BizTransactionID;
                            dbtxn.Add(bizEvent.Cheques[0]);

                            success = bizEvent.Cheques[0].Save();
                        }
                        else    // else write new cheque
                        {
                            ChequeEntity cheque = new ChequeEntity();
                            cheque.Drawer = dr["Drawer"].ToString();
                            cheque.BankName = dr["BankName"].ToString();
                            cheque.ChequeNo = dr["ChequeNo"].ToString();
                            cheque.BSB = dr["BSB"].ToString();
                            cheque.Account = dr["Account"].ToString();
                            cheque.BizTransactionID = biztxn.BizTransactionID;

                            dbtxn.Add(cheque);

                            success = cheque.Save();
                        }
                    }

                    account.Balance = account.Balance + Convert.ToDecimal(dr["AmountCleared"]);
                    account.UpdatedDate = DateTime.Now;
                    account.UpdatedUserID = userID;

                    dbtxn.Add(account);

                    success = account.Save();

                    //If there aren't any, then write BizEvents:    BizEventType___To_Be_Allotted
                    BizEventCollection bizEventsToAllot = new BizEventCollection();
                    IPredicateExpression filter = new PredicateExpression();
                    filter.Add(PredicateFactory.CompareValue(BizEventFieldIndex.ParentBizEventID, ComparisonOperator.Equal, bizEvent.BizEventID));
                    filter.Add(PredicateFactory.CompareValue(BizEventFieldIndex.BizEventTypeID, ComparisonOperator.Equal, Constants.BizEventType___To_Be_Allotted));
                    bizEventsToAllot.GetMulti(filter);

                    if(bizEventsToAllot.Count == 0)
                    {
                        switch(this.FundProductAllotmentTypeID)
                        {
                            case Constants.FundProductAllotmentType___Investor_Selection:
                                if(dvFundProducts != null)
                                {
                                    int iBizEventID = 0;
                                    foreach(DataRow drToAllot in dvFundProducts.Table.Rows)
                                    {
                                        if(dr["BizEventID"] != Convert.DBNull)
                                            iBizEventID = Convert.ToInt32(dr["BizEventID"]);

                                        if(!bizEvent.createNewToBeAlloted
                                            (
                                            userID,
                                            iBizEventID,
                                            this.FundID,
                                            Convert.ToInt32(drToAllot["FundProductID"]),
                                            drToAllot["Name"].ToString(),
                                            DateTime.Now,
                                            Convert.ToDecimal(drToAllot["Amount"]),
                                            account.AccountID,
                                            dbtxn,
                                            true,
                                            out sError
                                            ))
                                        {
                                            throw new Exception(sError);
                                        }
                                    }
                                }
                                break;
                            case Constants.FundProductAllotmentType___Split_By_Percentages:
                                DataTable dt = this.getAvailableProducts(this.FundID, Convert.ToDecimal(dr["AmountCleared"]));
                                foreach(DataRow drToAllot in dt.Rows)
                                {
                                    if(!bizEvent.createNewToBeAlloted
                                        (
                                        userID,
                                        bizEvent.BizEventID,
                                        FundID,
                                        Convert.ToInt32(drToAllot["FundProductID"]),
                                        drToAllot["Name"].ToString(),
                                        DateTime.Now,
                                        Convert.ToDecimal(drToAllot["Amount"]),
                                        account.AccountID,
                                        dbtxn,
                                        true,
                                        out sError
                                        ))
                                    {
                                        throw new Exception(sError);
                                    }
                                }
                                break;
                        }
                    }

                    //Set Status to complete for eventual NoD
                    if(dr["BizEventID"] != Convert.DBNull)
                    {
                        bizEvent.BizEventStatusID = Constants.BizEventStatus___Complete;
                        dbtxn.Add(bizEvent);
                        bizEvent.Save();
                    }

                    bizEvent = null;
                }

                if(forValidation)
                    dbtxn.Rollback();
                else
                    dbtxn.Commit();
                success = true;
            }
            catch(Exception ex)
            {
                dbtxn.Rollback();
                sError = ex.Message;
                success = false;
            }
            finally
            {
                dbtxn.Dispose();
            }
            return success;
        }






createNewToBeAlloted
Code:

        public bool createNewToBeAlloted(
            int userID,
            int parentBizEventID,
            int fundID,
            int fundProductID,
            string fundProductName,
            DateTime transactionDate,
            decimal amount,
            int accountInvestorFundID,
            Transaction dbtxn,
            bool noBizEventIfAmountIsZero,
            out string sError
            )
        {
            bool success = false;
            sError = "";

            if(noBizEventIfAmountIsZero && amount == 0)
            {
                success = true;
                return success;
            }

            try
            {
                FundProductEntity fundproduct = new FundProductEntity(fundProductID);
                if(!fundproduct.validateFundProductData(transactionDate, amount, out sError))
                {
                    throw new Exception(sError);
                }

                BizEventEntity bizeventAllott = new BizEventEntity();
                if(parentBizEventID != 0)
                    bizeventAllott.ParentBizEventID = parentBizEventID;
                bizeventAllott.CreatedDate = DateTime.Now;
                bizeventAllott.CreatedUserID = userID;
                bizeventAllott.BizEventTypeID = Constants.BizEventType___To_Be_Allotted;
                bizeventAllott.FundID = fundID;
                bizeventAllott.FundProductID = fundProductID;
                bizeventAllott.Amount = amount;
                bizeventAllott.BizEventDateTime = DateTime.Now;
                bizeventAllott.BizEventStatusID = Constants.BizEventStatus___Pending;
                bizeventAllott.FromAccountID = accountInvestorFundID;
                bizeventAllott.ToAccountID = fundproduct.Account.AccountID;
                
                if(dbtxn != null)
                    dbtxn.Add(bizeventAllott);

                success = bizeventAllott.Save();

                bizeventAllott = null;
                fundproduct = null;
            }
            catch(Exception ex)
            {
                dbtxn.Rollback();
                sError = ex.Message;
                success = false;
            }
            finally
            {
                dbtxn.Dispose();
            }
            return success;
        }
Rob (User)   Posted on: 10-May-2005 12:10:45.
Stop everything!

I found the problem. Forget all my theories. I was dumb enough to dispose the transaction object in the "child" method.

All my fault, I hope nobody spent any time reading all that code.

Sorry!

!Rob


Otis (LLBLGen Pro Team)   Posted on: 10-May-2005 12:42:49.
Rob wrote:
Stop everything!

I found the problem. Forget all my theories. I was dumb enough to dispose the transaction object in the "child" method.

All my fault, I hope nobody spent any time reading all that code.

Sorry!

!Rob

I saw all that code so I bookmarked it for later today Regular Smiley. Glad you found it yourself and everything works again! Regular Smiley