E10 Custom code on UD03 Form

We are getting errors about data is being updated by another. I have narrowed it down to the following code snipet. Is there a OTrans.refresh I should add? This is legacy code that I am working with, so pick away. lol

   private void UpdateNotificationDates(string type)
        {
            try
            {
                EpiDataView edvUD03 = ((EpiDataView)(oTrans.EpiDataViews["UD03"]));
                System.Data.DataRow edvUD03Row = edvUD03.CurrentDataRow;

                if (edvUD03Row != null)
                {
                    EpiDataView updateView = (EpiDataView)oTrans.EpiDataViews["UD03"];
                    DataRow updateRow = updateView.CurrentDataRow;

                    UD03Adapter ud03update = new UD03Adapter(oTrans);
                    ud03update.BOConnect();

                    updateRow.BeginEdit();

                    if (type == "Tasks") updateRow["Date06"] = DateTime.Now;
                    else if (type == "Approval") updateRow["Date07"] = DateTime.Now;
                    else if (type == "Implementation") updateRow["Date08"] = DateTime.Now;

                    updateRow.EndEdit();
                    ud03update.Update();
                }

                else MessageBox.Show("Warning - edvUD03Row is NULL");
            }

            catch (Exception ex) { MessageBox.Show("Exception thrown by UpdateNotificationDates: " + ex.Message); }
        }

Those errors are annoying. I don’t have an answer for you but this code looks a bit redundant. edvUD03 and updateView are the same beast. No need to make the second reference. Same for the row.

I’ve heard rumor that BeginEdit and EndEdit are unneeded in this case. Try that.

Wait - I just looked again. ud03update.Update() doesn’t do anything. You are updating an empty dataset (in the adapter)

Ken
Don’t instanciate your own adapter.
The issue here is that you have the record on the screen under your current session / adapter then in your code you are instanciating a new UD03 to change the data. This leaves the data on the screen in am “old” state so that when you try to modify the epidataview it sees a difference between the record you changed with your new adapter and the record currently loaded on the screen.
Instead of doing ud03update adapter = new UD03Adapter()… You should get a hold of the current adapter that you are already working with.
You do that by pulling it from oTrans by name
Like this
Mycurrentadapter = (UD03Adapter)csm.TransAdapterHT[“oTrans_Adapter”];

Mind you I’m typing on my phone but you get the gist

-Jose

There is code that sort of does what you are saying. Are both lines needed. Arrghh this code is messed up. 10K lines of spaghetti

    EpiDataView edv_UD03;

	public void InitializeCustomCode()
        {
            #region Initialize local variables

            this.edv_UD03 = ((EpiDataView)(this.oTrans.EpiDataViews["UD03"]));
            this.oTrans_adapter = ((EpiBaseAdapter)(this.csm.TransAdaptersHT["oTrans_adapter"]));

            #endregion
}

Ok so that code gets a hold of the current UD03 adapter. See where it says (EpiBaseAdapter). You can replace that with UD03Adapter and then use oTrans_adapter everywhere else in your code as the UD03 adapter.

IN your other code, the Commented lines need to go and need to be replaced with the above oTrans_Adapter.

private void UpdateNotificationDates(string type)
        {
            try
            {
                EpiDataView edvUD03 = ((EpiDataView)(oTrans.EpiDataViews["UD03"]));
                System.Data.DataRow edvUD03Row = edvUD03.CurrentDataRow;

                if (edvUD03Row != null)
                {
                    EpiDataView updateView = (EpiDataView)oTrans.EpiDataViews["UD03"];
                    DataRow updateRow = updateView.CurrentDataRow;

                    //Don't make a new Adapter Here... Causes Issues
                    /*
                    UD03Adapter ud03update = new UD03Adapter(oTrans);
                    ud03update.BOConnect(); */

           

                    updateRow.BeginEdit();

                    if (type == "Tasks") updateRow["Date06"] = DateTime.Now;
                    else if (type == "Approval") updateRow["Date07"] = DateTime.Now;
                    else if (type == "Implementation") updateRow["Date08"] = DateTime.Now;

                    updateRow.EndEdit();
                    //Use oTrans_Adapter instead
                    oTrans_Adapter.Update();
                }

                else MessageBox.Show("Warning - edvUD03Row is NULL");
            }

            catch (Exception ex) { MessageBox.Show("Exception thrown by UpdateNotificationDates: " + ex.Message); }
        }

OK based on what you said. This issue is here as well :frowning:
I will start to remove the adapter lines and use the global oTrans one.

 private void oTrans_adapter_BeforeAdapterMethod(object sender, BeforeAdapterMethodArgs args)
        {
            switch (args.MethodName)
            {
                case "Update":
                    try
                    {
                        EpiDataView edvUD03 = ((EpiDataView)(oTrans.EpiDataViews["UD03"]));
                        DataRow edvUD03Row = edvUD03.CurrentDataRow;

                        if (edvUD03Row != null)
                        {
                            if (!ECR_Status_IsNew) CheckForChanges();

                            EpiDataView updateView = (EpiDataView)oTrans.EpiDataViews["UD03"];
                            DataRow updateRow = updateView.CurrentDataRow;

                            UD03Adapter ud03update = new UD03Adapter(oTrans);
                            ud03update.BOConnect();

                            updateRow.BeginEdit();

                            updateRow["ShortChar15"] = currentUser["DcdUserID"];
                            updateRow["Date09"] = DateTime.Now;

                            if (ECR_Status_IsNew)
                            {
                                updateRow["ShortChar05"] = currentUser["DcdUserID"];
                                updateRow["Date01"] = DateTime.Now;
                            }

                            if (affectsFormFitFunction) updateRow["ShortChar20"] = "Yes";
                            else updateRow["ShortChar20"] = "No";

                            if (!ECR_Status_HasTasks) updateRow["ShortChar18"] = "Inactive";
                            else
                            {
                                if (!tasksComplete_Approval) updateRow["ShortChar18"] = "Approval";
                                else if (tasksComplete_Approval && !tasksComplete_Standard) updateRow["ShortChar18"] = "Priority" + activePriority.ToString();
                                else if (tasksComplete_Approval && tasksComplete_Standard && !tasksComplete_Implementation) updateRow["ShortChar18"] = "Implementation";
                            }

                            if (ECR_Status_WasModified) updateRow["CheckBox20"] = false;

                            if (ECR_Status_HasTasks) updateRow["CheckBox24"] = true;

                            updateRow.EndEdit();
                            ud03update.Update();

                            ud03update.Dispose();
                        }

                        ECR_Status_IsNew = false;
                    }

                    catch (Exception ex) { MessageBox.Show("Exception thrown by oTrans_adapter_BeforeAdapterMethod: " + ex.Message); }

                    break;

                default:
                    break;
            }
        }

Yup, I’m not sure how that ever worked reliably… you should have gotten that Record Modified by XYZ all the time. One thing to remember DO NOT dispose of oTrans_Adapter … unless you want lots and lots of pain.
Remember you are getting a hold of the Current form’s adapter, so if you dispose of it… you dispose of the one in the form… and nothing will work :wink:

2 Likes

Thanks.

Yes folks have been saying the form gives errors all the time, but they have been using it for three years like this. I am starting to make changes, but there are many moving parts. When enhancing old code, I try not to recreate outside the scope of the request due to coding style This might be an exception.