Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Tips on parent-child bizobj to share db transaction
Message
Information générale
Forum:
ASP.NET
Catégorie:
The Mere Mortals .NET Framework
Divers
Thread ID:
01235772
Message ID:
01237617
Vues:
11
Kevin,

I got the program to autopopulate the the foreign key fields in the child objects. I re-coded all the modification that are required to setup child objects and it's now working, not sure what I did wrong the first time around.

Unfortunately, I've run into another issue I could use your help with. I have a form with controls that are mapped to 3 different business objects. These are the same objects as above. Obj1, Obj2, Obj3. Obj2 is a child of Obj1 and Obj3 is a child of Obj1. When the user submits the form, I save Obj1, if SaveDataResults == Passed I save Obj2, If SaveDataResults == Passed I save Obj3. The behavior that I want is for them all to use the same transaction and when there is a failed save in obj2 or Obj3 the transaction is rolled back.

I believe the documentation states that if you register a business object as a child of another business object, they automatically participate in the transaction of the parent. But when I tried this, I found that saves within the transaction were getting committed even when there was a failed save in Obj2 or Obj3.

Then I tried to explicitly start and commit (or rollback) a transaction on Obj1, with the understanding that the child objects would use the parent transaction. This works when there are no validation problems. When there are validation problems and I attempt to save the second time after fixing the validation issue, I get the following: "ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."

Here's my code that produces that error:
  protected void Page_Load(object sender, EventArgs e)
    {
        this.oStates = (StateNames)this.RegisterBizObj(new StateNames());

        this.oObject1 = (Object1)this.RegisterBizObj(new Object1());
        this.oObject3 = (Object3)this.RegisterBizObj(new Object3());
        this.oObject2 = (Object2)this.RegisterBizObj(new Object2());

        this.oObject1.RegisterChildBizObj(oObject2);
        this.oObject1.RegisterChildBizObj(oObject3);


        if (!IsPostBack)
        {
            this.oStates.GetAllEntities();

            oObject1.NewRow();
            oObject2.NewRow();
            oObject3.NewRow();

            Session["dsObject1"] = oObject1.DataSet;
            Session["dsObject2"] = oObject2.DataSet;
            Session["dsObject3"] = oObject3.DataSet;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DataSet dsNewObj1;
        DataSet dsNewObj2;
        DataSet dsNewObj3;
        mmSaveDataResult Result;
        bool doRollback = false;
        int saltSize = 5;
        string salt = CreateSalt(saltSize);
        string passwordHolder;


        // get the new entity datasets from the session 
        dsNewObj1 = (DataSet)Session["dsObject1"];
        dsNewObj2 = (DataSet)Session["dsObject2"];
        dsNewObj3 = (DataSet)Session["dsObject3"];

        // start a transaction for each insert
        oObject1.TransactionBegin();

        try
        {
            // validate that the password fields match
            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                // add my own broken rule
                oObject1.Rules.AddErrorProviderBrokenRule("password", "passwords do not match", "WebUsers");
                Result = mmSaveDataResult.RulesBroken;
            }
            else
            {
                // passwords match
                // encrypt the password for storage
                string passwordHash = CreatePasswordHash(txtPassword.Text, salt);

                passwordHolder = txtPassword.Text;

                // replace the password in the dataset with the hashed password
                txtPassword.Text = passwordHash;

                // attempt to save the user to the database
                Result = this.Save(this.oObject1, dsNewObj1, this.oObject1.TableName);

                // set it back to what it was
                txtPassword.Text = passwordHolder;
            }

            if (Result == mmSaveDataResult.RulesPassed)
            {
                // add 30 days to surveyor renewal date while accpeting checks
                dsNewObj2.Tables["Surveyors"].Rows[0]["renewal_date"] = DateTime.Now.Date.AddDays(31);

                // try to save surveyor
                if (this.Save(this.oObject2, dsNewObj2, this.oObject2.TableName) !=
                        mmSaveDataResult.RulesPassed)
                {
                    doRollback = true;
                }
                else
                {
                    UpdateCoordinates(dsNewObj3.Tables[oObject3.TableName].Rows[0]);

                    // try to save contact information
                    if (this.Save(this.oObject3, dsNewObj3, this.oObject3.TableName) !=
                            mmSaveDataResult.RulesPassed)
                    {
                        doRollback = true;
                    }
                }

                // if something didn't work, rollback database transaction
                if (doRollback)
                {
                    oObject1.TransactionRollback();
                }
                else
                {
                    // everything worked, commit the database transactions
                    oObject1.TransactionCommit();

                    // store userid in the session
                    Session["uid"] = oObject1.Entity.webuser_id;
                    Session["uname"] = oObject1.Entity.username;

                    Response.Redirect("PaymentInfo.aspx");
                }
            }
        }
        catch (Exception ex)
        {
            oObject1.TransactionRollback();
            throw ex;
        }

    }
Any help you could give would be greatly appreciated.

Thank You,
Govinda Berrio
GKG Inc.
Providence, RI
http://www.gkginc.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform