This topic is locked

Get key field on copy

11/19/2018 1:02:11 PM
ASPRunner.NET General questions
T
Tim author

Hi all,
I have a list page that allows users to copy a record. I'd like to grab the ID of the record being copied and save it into a session variable. (I will then use this for a select statement so I can do an insert into a details table, giving the new master record the same details records as the copied master).
I assume I will do this in the "Copy page:Onload" event, but I can't figure it out. I see in the help info at the top of the event page it says "where - WHERE clause that points to the record to be copied. Example: ID=19". So there it is, but how to I assign this to a session? My lame attempt was this:
XSession.Session["copyid"]=where;
This didn't give me errors, but also didn't work.
UPDATE

Before I posted this I did one last search and found this: http://asprunner.com/forums/topic/19507-tip-copy-child-records-when-master-record-is-copied/
It seems I need the "Add page: Before process" event and this code:
if (@$_REQUEST["copyid1"])

$_SESSION["copyid1"] = $_REQUEST["copyid1"];
But that's PHP. Can I/how can I do that in C#?
Thanks for any help.

Tim

A
Arkie 11/19/2018

Maybe I can get you started as I have a copy record working, but there a couple of gotchas which seem to be related to my lack of knowledge about version 10.
I used the code for a custom record update in an edit page event such as:

++++

dynamic tblitems = GlobalVars.dal.Table("items");

tblitems.Value["itemcode"] = values["itemcode"];

tblitems.Value["description"] = values["description"];

tblitems.Value["vendor"] = values["vendor"];

tblitems.Add();

return false;

++++

I get the copy record part of this, but in version 10, I'm at a loss on how to implement it. This copies the chosen record, but I lose the Edit page function and I haven't figured out how to get both.

There is an option to have a "copy page" in the pages to build section and it puts a small icon next to the edit icon on a list page. BUT.. I haven't figured out how to use it. The icon gets created, but doesn't appear to know it's own destination.

The docs mention something about an "Add (in copy mode)", but don't find any info on it.
~Joe

admin 11/19/2018

The idea of having WHERE clause as a parameter is to let you execute SQL query to retrieve the original record.
So you can use something like this:

XSession.Session["copyid"] = tDAL.DBLookup(String.Format("select <keycolumn> from <tablename> where {0}"), where);


@Arkie,
I wish I understand what you trying to achieve or what causes the trouble. Your code has nothing to do with version 10. Probably start a new thread and be descriptive.

T
Tim author 11/20/2018

@Arkie
Thanks for the reply. I haven't tried to add a copy page in version 10, but will need to do so soon. Hopefully it works.
@admin
Thank you. I get it. The WHERE clause is literally "ID=19" and not just the value "19". Based on that, it does look like the code you provided is exactly what I need. Here is what I ended up with:
XSession.Session["copyid"] = tDAL.DBLookup(String.Format("select ID from dbo.StandingOrders where {0}"), where);
But when I build my project I get this error:
error CS1501: No overload for method 'DBLookup' takes 2 arguments
Maybe the double quote is in the wrong place...? I tried a couple of changes but couldn't get it. Can you help?
Thanks,

Tim

T
Tim author 11/20/2018

I am removing what I previously posted because I was wrong, it wasn't working. I still need to figure out why I get an error from the DBLookup.
Thanks,

Tim

admin 11/20/2018

A typo on my side. Here is the correct code:

XSession.Session["copyid"] = tDAL.DBLookup(String.Format("select <keycolumn> from <tablename> where {0}", where));
T
Tim author 11/21/2018

Thank you (I probably should have been able to figure that out), but now I'm getting an error in the published app. It seems like the "where" parameter is empty. I tried displaying it on the page and I get nothing. And the here is the error I get when I click the copy link:
Value cannot be null.

Parameter name: args
Line 26: XSession.Session["copyid"] = tDAL.DBLookup(String.Format("select ID from dbo.StandingOrders where {0}", where));
I published a test app to the demo site to demonstrate the issue. Here is the link:
http://demo.asprunner.net/tim_carter_mndigerati_com/Testsv98/dbo_Tests/list
Click the copy link to see the error.
I am letting you know this for your info, but for my current needs, I think I have a solution. I realized that there is a "post" value with the copied field's key, so it looks like I can do this:
XSession.Session["copyid"]=MVCFunctions.postvalue("copyid1");
So, unless you tell me this is not a good solution, I think I'll move forward with this.
Thanks, as always!

Tim

admin 11/21/2018

I can confirm that in current version of ASPRunner.NET where is empty in CopyOnLoad event. This is a bug and we are working on it.
The method that you use should work just fine.