Transfer 2 Primary Keys from Master using Preview Row

Tips submitted by ASP.NET Maker users

Transfer 2 Primary Keys from Master using Preview Row

Postby xgis » Tue Aug 06, 2013 12:36 pm

Generally you use AutoFill and establish a Master/Child key relationship to assist in the transfer of Primary Key/s to the child table.
When you use the "Preview Row" extension and try to perform the same task it may not work. eg the main primary key has autofill set to true, but does not transfer the second primary key on ADD.
When you go to ADD new records to a "Preview Row" enabled Child table it is actually possible to transfer at least 2 primary keys without the need for code.

In my example I am transferring a normal integer based ID and a GUID.
The way this is achieved is by setting up two 1-Many relationships on the child table from the master table.
When you use Preview row links you can then ADD a new child record and the two primary keys transfer without issue.
To ensure the keys are not accidentally modified it is a good idea to set the field to read only. This can be done in the Row_Inserting event.
eg
GlobalID.ReadOnly = true;
xgis
 
Posts: 87
Joined: Thu Jan 17, 2013 10:40 pm

Re: Improved Code for Transfer of 2 Primary Keys from Master

Postby xgis » Thu Dec 26, 2013 11:06 pm

The following code excerpt replaces this method which more often than not does not work consistently;
To customise to your solution change "ClientFunctionGUID" to your GUID field. Change "ClientFunctionID" to your ParentID field.
Replace "VCWAMC.CD0ClientFunctions" with your <Schema.TableName>
It is important to note that the EDIT | Read Only checkboxes do NOT need to be selected when using code in this way.


// Row Rendered event
public void Row_Rendered() {

if (ClientFunctionGUID.EditValue != "")
{ //Get GUID (PK) from Master Table to Populate the Child Table Field (FK)
ClientFunctionGUID.EditValue = ew_ExecuteScalar("SELECT ClientFunctionGUID FROM VCWAMC.CD0ClientFunctions WHERE ClientFunctionID=" + ClientFunctionID.CurrentValue);
ClientFunctionGUID.ReadOnly = true;
OrderGUID.ReadOnly = true; //Only required if two GUID's are present in the same table where this GUID is the Unique GUID
}
else
{
ClientFunctionGUID.ReadOnly = true;
OrderGUID.ReadOnly = true; //Only required if two GUID's are present in the same table where this GUID is the Unique GUID
}
}
xgis
 
Posts: 87
Joined: Thu Jan 17, 2013 10:40 pm


Return to User Submited Tips (ASP.NET Maker)