Populating the ID and Audit Fields in DataPortal_Insert from Sproc Parameters

Populating the ID and Audit Fields in DataPortal_Insert from Sproc Parameters

Old forum URL: forums.lhotka.net/forums/t/7109.aspx


Smartie posted on Tuesday, June 16, 2009

Hey,

I have a class which inherits the BusinessBase, I am having some trouble with the DataPortal_Insert() sub.

Within this sub I have the code to exec the stored procedure on my database, but I need to update the IdProperty and the audit fields with the values returned from the stored procedure. The following is the code I have currently:
cm.ExecuteNonQuery()

LoadProperty(Of Integer)(IdProperty, CType(cm.Parameters.Item("@Id").Value, Integer))
LoadProperty(Of SmartDate)(DateAddedProperty, CType(cm.Parameters("@DateAdded").Value, Date))
LoadProperty(Of SmartInt)(ABUserIdProperty, New SmartInt(CType(cm.Parameters("@ABUserId").Value, Integer)))

LoadProperty(Of SmartDate)(DateUpdatedProperty, GetProperty(Of SmartDate)(DateAddedProperty))
LoadProperty(Of SmartInt)(UBUserIdProperty, GetProperty(Of SmartInt)(ABUserIdProperty))
I have run the debugger on this and the new id is within "CType(cm.Parameters.Item("@Id").Value, Integer)".

But it does not seem to update the class with the value, as when doing:

MyClass.Save()

And then checking the value of MyClass.Id, it's always 0.

I am having the same issue with updating and the audit fields.

Help.


Smartie replied on Tuesday, June 16, 2009

Forgot to say, I am using Csla 3.6.1.0

rsbaker0 replied on Tuesday, June 16, 2009

Are you sure the object you are trying to Save() is flagged as Dirty? I don't think LoadProperty marks an object as dirty, and since that was all I saw above I thought this might be something to look at...

Smartie replied on Tuesday, June 16, 2009

I am pretty sure it is marked as dirty as the insert code is being called.

How does it being dirty or not effect setting the properties using load property?

rsbaker0 replied on Tuesday, June 16, 2009

I see your stored procedure call above, but I don't see the Save() code afterwards.

Anyway, I was just thinking that calling Save() on a clean object is a NOP by default, but you are right about your Insert being called. Where do you Save the object with the values returned from the sp?

JoeFallon1 replied on Tuesday, June 16, 2009

This is the #1 bug in dev code:
MyClass.Save()

It is always wrong to do that!

It should always be:
myClass = MyClass.Save()

You have to remember that CSLA always returns a new instance of the object from the dataportal and you must update your references.

Once you fix this the issue you have will go away.

Joe

Smartie replied on Wednesday, June 17, 2009

Joe, you saved me, thank you :D

In our previous version of CSLA we just called save and that updated the object you called save on.

JoeFallon1 replied on Wednesday, June 17, 2009

Smartie:
Joe, you saved me, thank you :D

In our previous version of CSLA we just called save and that updated the object you called save on.


That is right - older code did not clone the object when it used the local dataportal. That is why your old code "worked". The problem is that it would be broken if you tried to enable a remote dataportal. By updating the framework Rocky exposed the bugs in your code at an earlier stage. Now remoting will work correctly for your app.

Joe

mddubs replied on Saturday, July 11, 2009

JoeFallon1:
This is the #1 bug in dev code: MyClass.Save() It is always wrong to do that! It should always be: myClass = MyClass.Save() You have to remember that CSLA always returns a new instance of the object from the dataportal and you must update your references. Once you fix this the issue you have will go away. Joe


Thank you!  I was having a dull moment and I'm glad it didn't turn into a dull hour :)

Copyright (c) Marimer LLC