Solved

Creating a UD table record using c# script in updateable dashboard

  • 20 December 2022
  • 3 replies
  • 429 views

Userlevel 1

I have an updateable dashboard where the user wants to record changes they have made, so I am thinking of writing this to a UD table on a button click. I am familiar with creating button click actions and using them to manipulate data within the dashboard, but creating a new record in a foreign table is somewhat beyond me. Would be grateful for any suggestions or sample code.

ERP 10.2.700

icon

Best answer by mwilson 20 December 2022, 16:39

View original

3 replies

Userlevel 4

The only example I have of doing this, was a case where we were setting up a parent/child relationship between the native EDV table (OrderRel) and the UD table.  Here’s the init:

 

// InitializeUD01Adapter runs during load of blank panel

    private void InitializeUD01Adapter()

    {

        //EpiMessageBox.Show("Reached InitializeUD01Adapter" );

        // Create an instance of the Adapter.

        this._ud01Adapter = new UD01Adapter(this.oTrans);

        this._ud01Adapter.BOConnect();

        // Add Adapter Table to List of Views

        // This allows you to bind controls to the custom UD Table

        this._edvUD01 = new EpiDataView();

        this._edvUD01.dataView = new DataView(this._ud01Adapter.UD01Data.UD01);

        this._edvUD01.AddEnabled = true;

        this._edvUD01.AddText = "New UD01";

        if ((this.oTrans.EpiDataViews.ContainsKey("UD01View") == false))

        {

            this.oTrans.Add("UD01View", this._edvUD01);

        }

        // Initialize EpiDataView field.

        this._edvOrderRel = ((EpiDataView)(this.oTrans.EpiDataViews["OrderRel"]));

        // Set the parent view / keys for UD child view

        string[] parentKeyFields = new string[3];

        string[] childKeyFields = new string[3];

        parentKeyFields[0] = "OrderNum";

        childKeyFields[0] = "Key1";

        parentKeyFields[1] = "OrderLine";

        childKeyFields[1] = "Key2";

        parentKeyFields[2] = "OrderRelNum";

        childKeyFields[2] = "Key3";

        this._edvUD01.SetParentView(this._edvOrderRel, parentKeyFields, childKeyFields);


        if ((this.oTrans.PrimaryAdapter != null))

        {

            //this.oTrans.PrimaryAdapter.GetCurrentDataSet(Ice.Lib.Searches.DataSetMode.RowsDataSet).Tables.Add(this._edvUD01.dataView.Table.Clone())

        }

    }

...and  here are the other methods used to create a UD record or otherwise manipulate it.

 

private void GetNewUD01Record()

    {

        //EpiMessageBox.Show("Reached GetNewUD01Record" );

        DataRow parentViewRow = this._edvOrderRel.CurrentDataRow;

        // Check for existence of Parent Row.

        if ((parentViewRow == null))

        {

            return;

        }

        if (this._ud01Adapter.GetaNewUD01())

        {

            string ordernum = parentViewRow["OrderNum"].ToString();

            string orderline = parentViewRow["OrderLine"].ToString();

            string orderrelnum = parentViewRow["OrderRelNum"].ToString();

            string shipToCustNum = parentViewRow["ShipToCustNum"].ToString();

            SearchOnCustCntAdapterFillDropDown(shipToCustNum);

            // Get unique row count id for Key5 

            int rowCount = this._ud01Adapter.UD01Data.UD01.Rows.Count;

            int lineNum = rowCount;

            /* The following lines (not used since no need for unique record identifier)

            bool goodIndex = false;

            while ((goodIndex == false))

            {

                // Check to see if index exists

                DataRow[] matchingRows = this._ud01Adapter.UD01Data.UD01.Select("Key5 = \'" + lineNum.ToString() + "\'");

                if ((matchingRows.Length > 0))

                {

                    lineNum = (lineNum + 1);

                } else

                {

                    goodIndex = true;

                }

            }

            */

            // Set initial UD Key values when UD record created

            // Set number06 .. number08 to the numeric forms for linkability to BAQs

            DataRow editRow = this._ud01Adapter.UD01Data.UD01.Rows[(rowCount - 1)];

            editRow.BeginEdit();

            editRow["Key1"] = ordernum;

            editRow["Number06"] = parentViewRow["OrderNum"];

            editRow["Key2"] = orderline;

            editRow["Number07"] = parentViewRow["OrderLine"];

            editRow["Key3"] = orderrelnum;

            editRow["Number08"] = parentViewRow["OrderRelNum"];

            editRow["Key4"] = string.Empty;

            editRow["Key5"] = string.Empty; //lineNum.ToString();

            //editRow["Checkbox03"] = false;             not needed for now because default is to clear Final Shipment Scheduled

            editRow.EndEdit();

            // Notify that data was updated.

            this._edvUD01.Notify(new EpiNotifyArgs(this.oTrans, (rowCount - 1), this._edvUD01.Column));

        }

    }

    private void SaveUD01Record()

    {

        // Save adapter data

        this._ud01Adapter.Update();

    }

    private void DeleteUD01Record()

    {

        // Check to see if deleted view is ancestor view

        bool isAncestorView = false;

        Ice.Lib.Framework.EpiDataView parView = this._edvUD01.ParentView;

        while ((parView != null))

        {

            if ((this.oTrans.LastView == parView))

            {

                isAncestorView = true;

                break;

            } else

            {

                parView = parView.ParentView;

            }

        }

        // If Ancestor View then delete all child rows

        if (isAncestorView)

        {

            DataRow[] drsDeleted = this._ud01Adapter.UD01Data.UD01.Select("Key1 = \'" + this._Key1UD01 + "\' AND Key2 = \'" + this._Key2UD01 + "\' AND Key3 = \'" + this._Key3UD01 + "\' AND Key4 = \'" + this._Key4UD01 + "\'");

            for (int i = 0; (i < drsDeleted.Length); i = (i + 1))

            {

                this._ud01Adapter.Delete(drsDeleted[i]);

            }

        } else

        {

            if ((this.oTrans.LastView == this._edvUD01))

            {

                if ((this._edvUD01.Row >= 0))

                {

                    DataRow drDeleted = ((DataRow)(this._ud01Adapter.UD01Data.UD01.Rows[this._edvUD01.Row]));

                    if ((drDeleted != null))

                    {

                        if (this._ud01Adapter.Delete(drDeleted))

                        {

                            if ((_edvUD01.Row > 0))

                            {

                                _edvUD01.Row = (_edvUD01.Row - 1);

                            }

                            // Notify that data was updated.

                            this._edvUD01.Notify(new EpiNotifyArgs(this.oTrans, this._edvUD01.Row, this._edvUD01.Column));

                        }

                    }

                }

            }

        }

    }

HTH

...Monty.

Userlevel 1

Thanks Monty, I will have a crack at this… although it looks rather intimidating!

Kind regards,

Peter

Userlevel 1

Hi Monty,

Once again, many thanks. I adapted your code and it works fine, after a few attempts. I only needed a couple of snippets from what you posted.

Merry Christmas!

Peter

Reply