oTrans.Update() resets value

epicor
customization
e10

(Daniel Garstone) #1

Hi,

On Purchase Order Entry, we have custom fields to change the Amend Date with reasoning. Behind this button is some code to perform the actual update. The relevant part of this code is:

row["Date01"] = (DateTime)newAmendDate;
var success = oTrans.Update();
oTrans.NotifyAll(true);

As you can see its very simple and nothing complex, however in some circumstances which are outlined below, the Update() method will reset the value of the row and it will not be saved to the database. I have tried using BeginEdit() and EndEdit() as well but to no avail.

Circumstances:

This works:

  1. Load PO
  2. Select Line
  3. Modify PO Amend Date
  4. Make another change
  5. Save Form
  6. Select New Line
  7. Modify PO Amend Date

This method will make all changes as expected to the Database.

This does not work:

  1. Load PO
  2. Select Line
  3. Modify PO Amend Date
  4. Select Another Line
  5. Modify PO Amend Date – This one fails.

I cant ask users to Save the form between lines without changing anything else as there is no binding on the field as it is all done in the code, it doesnt recognise a change and therefore Update() will not be triggered.

I am really stumped by this and have lost many hours on it. If anybody knows what may cause this I will be forever grateful.

Thanks.

P.S. Epicor version 10.1.500


(Surendra Pal) #2

Hi Daniel,

are you not using Epicor binding? if not paste your code here to see what wrong your doing.


(Daniel Garstone) #3

Hi,

No i am not using epicor binding. I was not the original developer behind this so cannot explain the reason why. The full code for what happens on button click is as follows:

var edvPORel = (EpiDataView)oTrans.EpiDataViews["PORel"];
var row = edvPORel.CurrentDataRow;
if (newAmendDate != null && reason != "")
{
    try
    {
    var callContext = (EpiDataView)oTrans.EpiDataViews["CallContextClientData"];
    var userName = callContext.dataView[callContext.Row]["CurrentUserId"];
						
    var poNum = row["PONum"];
    var poLine = row["POLine"];
    var poRel = row["PORelNum"];
    var oldAmendDate = row["Date01"];
    row["Date01"] = (DateTime)newAmendDate;
    var success = oTrans.Update();
    oTrans.NotifyAll(true);
			
    using (SqlConnection connection = new SqlConnection(CONNECTION_STRING))
    {
        string qry = @"INSERT INTO nexus_po_date_log (PONum, POLine, PORelNum, OldDate, NewDate, Reason, Username) 
	    VALUES (@poNum, @poLine, @poRel, @oldDate, @newDate, @reason, @username)";
	    SqlCommand cmd = new SqlCommand(qry, connection);
	    cmd.Parameters.AddWithValue("@poNum", poNum);
	    cmd.Parameters.AddWithValue("@poLine", poLine);
	    cmd.Parameters.AddWithValue("@poRel", poRel);
	    cmd.Parameters.AddWithValue("@oldDate", oldAmendDate);
	    cmd.Parameters.AddWithValue("@newDate", newAmendDate);
	    cmd.Parameters.AddWithValue("@reason", reason);
	    cmd.Parameters.AddWithValue("@username", userName);
				
	    cmd.Connection.Open();
	    cmd.ExecuteNonQuery();
	}					
}
catch (Exception ex)
{
    throw ex;
}
} 
else 
{
    MessageBox.Show("Please select a date and provide a reason for change.");
}

The majority of this is purely logging which happens after the fact but I hope this shows everything you need.


(Jose C Gomez) #4

Try using beginedit and endedit to alert the form of the change

-Jose


(Daniel Garstone) #5

I did try as below but Im not sure if im using it correct. Also tried using SetModified() and AcceptChanges()

row.BeginEdit();
row["Date01"] = (DateTime)newAmendDate;
row.EndEdit();
row.AcceptChanges();
row.SetModified();
var success = oTrans.Update();
oTrans.NotifyAll(true);

(Jose C Gomez) #6

Oh yikes!
You are writing to a sql db … And directly from the UI :scream::scream::scream:
Hmm try setting the rowmod if the row


(Daniel Garstone) #7

If i add in row.RowMod = “U”; i get the error:

'System.Data.DataRow' does not contain a definition for 'RowMod' and no extension method 'RowMod' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?)

(Jose C Gomez) #8

RowMod is just another field you need to quote it


(Daniel Garstone) #9

OK using:
row["RowMod"] = "U";

it compiles fine now however, the issue still persists


(Surendra Pal) #10

follow these steps

  1. add update your code Initialize

    private EpiDataView _edvPORel;
    

    public void InitializeCustomCode()
    {
    // ** Wizard Insert Location - Do not delete ‘Begin/End Wizard Added Variable Initialization’ lines **
    // Begin Wizard Added Variable Initialization

     // End Wizard Added Variable Initialization
     this._edvPORel = ((EpiDataView)(this.oTrans.EpiDataViews["PORel"]));
     // Begin Wizard Added Custom Method Calls
    
     // End Wizard Added Custom Method Calls
    

    }

  2. public void DestroyCustomCode()
    

    {
    // ** Wizard Insert Location - Do not delete ‘Begin/End Wizard Added Object Disposal’ lines **
    // Begin Wizard Added Object Disposal

     // End Wizard Added Object Disposal
     this._edvPORel = null;
     // Begin Custom Code Disposal
    
     // End Custom Code Disposal
    

    }

3 var edvPORel = (EpiDataView)oTrans.EpiDataViews[“PORel”];
var row = edvPORel.CurrentDataRow;
if (newAmendDate != null && reason != “”)
{
try
{
var callContext = (EpiDataView)oTrans.EpiDataViews[“CallContextClientData”];
var userName = callContext.dataView[callContext.Row][“CurrentUserId”];

var poNum = row["PONum"];
var poLine = row["POLine"];
var poRel = row["PORelNum"];
var oldAmendDate = row["Date01"];
DataRow editRow = this._edvPORel.CurrentDataRow;;
editRow.BeginEdit();
editRow["Date01"] = (DateTime)dt_AmendDate.Value;
editRow.EndEdit();
this.oTrans.Update();
this._edvPORel.Notify(new EpiNotifyArgs(this.oTrans, this._edvPORel.Row, this._edvPORel.Column));
		
using (SqlConnection connection = new SqlConnection(CONNECTION_STRING))
{
    string qry = @"INSERT INTO nexus_po_date_log (PONum, POLine, PORelNum, OldDate, NewDate, Reason, Username) 
    VALUES (@poNum, @poLine, @poRel, @oldDate, @newDate, @reason, @username)";
    SqlCommand cmd = new SqlCommand(qry, connection);
    cmd.Parameters.AddWithValue("@poNum", poNum);
    cmd.Parameters.AddWithValue("@poLine", poLine);
    cmd.Parameters.AddWithValue("@poRel", poRel);
    cmd.Parameters.AddWithValue("@oldDate", oldAmendDate);
    cmd.Parameters.AddWithValue("@newDate", newAmendDate);
    cmd.Parameters.AddWithValue("@reason", reason);
    cmd.Parameters.AddWithValue("@username", userName);
			
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
}					

}
catch (Exception ex)
{
throw ex;
}
}
else
{
MessageBox.Show(“Please select a date and provide a reason for change.”);
}

PS let me know if you have any question


(Daniel Garstone) #11

Unfortunately updating my code as you described didnt work. I had to make slight changes as its a static class. Could the static class be the issue? Im not sure if it needs to be static or not and if not im not sure why it was created as static


(Surendra Pal) #12

Hi Daniel,

I am not sure why and how this code created. if you have any documentation share to get more idea or ping me on Skype surendrpal to review your code.

Thanks
Surendra Pal


(Surendra Pal) #13

@daroga please Mark this as solution and post final solution as we discussed on skype for others help in future