Lookups API ApplyChanges

Discussions related to order tracking development with the ProForm module.

Moderator: Phil Barton

Post Reply
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Lookups API ApplyChanges

Post by czentman »

Does anyone know why I this code "lookups.ApplyChanges(table)" I get an error unable to castexception after a delete of a record? Also, it doesn't seem to really commit the changes after an update either. The update works, but when it comes to committing it, it already reverts back.
Mark McKenna

Re: Lookups API ApplyChanges

Post by Mark McKenna »

Do you have example code that we could look at? When I do the following, the row is deleted and committed. Also, any additional details about the exception would be helpful.

Code: Select all

// delete first row in Pest Inspector lookup table
Select.Client.SelectServer server = _serviceProvider.GetService( typeof( Select.Client.SelectServer ) ) as Select.Client.SelectServer;
SoftPro.OrderTracking.Client.Lookups lookups = server.GetService<SoftPro.OrderTracking.Client.Lookups>( );
using ( System.Data.DataTable table = lookups.GetTable( "Pest Inspector" ) )
{
	if ( table.Rows.Count > 0 )
	{
		System.Data.DataRow row = table.Rows[0];
		row.Delete( );
		lookups.ApplyChanges( table );
	}
}
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Lookups API ApplyChanges

Post by czentman »

I was able to get it to work using your code. So I tried changing little by little. I need to refresh the table again before committing the change. THat works. But it's a little strange because the table is the data from the beginning so why do you have to re - GetTable? it works so whatever. thanks
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Lookups API ApplyChanges

Post by czentman »

can you send me a correct code snippet, tested and works, for an edit to a record and commit the changes.
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Lookups API ApplyChanges

Post by czentman »

Ignore previous post. I figured out a different way. Instead of using DirectCast. I created another lookup value type variable, sent the variable and then set the row.item equal to the variable. That worked, great.
Mark McKenna

Re: Lookups API ApplyChanges

Post by Mark McKenna »

I'll answer your request anyway for the sake of other readers. I'm not sure what you mean by having to re-get the DataTable (I understand what you're doing - I'm not understanding why). Our ApplyChanges method relies entirely on the row states of the temporary DataTable that we give you (the reference to which you're passing back). Internally, we'll commit the changes to that DataTable for you once we've evaluated and processed the data on our end - that's why you can't call DataTable.AcceptChanges yourself before giving it back to us - we wouldn't know what's been added, changed, or deleted. But after you've called our ApplyChanges method, the DataTable should be current and "fully accepted" for the edit/apply process to happen all over again.

Code: Select all

// edit the first row in Pest Inspector lookup table
Select.Client.SelectServer server = _serviceProvider.GetService( typeof( Select.Client.SelectServer ) ) as Select.Client.SelectServer;
SoftPro.OrderTracking.Client.Lookups lookups = server.GetService<SoftPro.OrderTracking.Client.Lookups>( );
using ( System.Data.DataTable table = lookups.GetTable( "Pest Inspector" ) )
{
	if ( table.Rows.Count > 0 )
	{
		System.Data.DataRow row = table.Rows[0];
		row.BeginEdit( );
		// change the name of the contact
		SoftPro.OrderTracking.Common.LookupValue value = row["Name"] as SoftPro.OrderTracking.Common.LookupValue;
		value.Value = "My New Name";
		row.EndEdit( );
		lookups.ApplyChanges( table );
	}
}
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Lookups API ApplyChanges

Post by czentman »

can you send the code for adding as well?
Mark McKenna

Re: Lookups API ApplyChanges

Post by Mark McKenna »

The Lookups service exposes the underlying data using ADO.NET constructs, so adding a row is similar to editing. The ADO.NET NewRow() method will provide you a DataRow of the correct schema that contains default LookupValue objects in each DataColumn. So you would proceed by simply editing that new row like in my previous example.

In our forthcoming Morgan release, you would do something like this.

Code: Select all

// create a new entry in Pest Inspector lookup table
Select.Client.SelectServer server = _serviceProvider.GetService( typeof( Select.Client.SelectServer ) ) as Select.Client.SelectServer;
SoftPro.OrderTracking.Client.Lookups lookups = server.GetService<SoftPro.OrderTracking.Client.Lookups>( );
using ( System.Data.DataTable table = lookups.GetTable( "Pest Inspector" ) )
{
	// create the new row with the correct schema and default values
	System.Data.DataRow row = table.NewRow( );

	// the LookupValue reference we'll reuse here
	SoftPro.OrderTracking.Common.LookupValue value = null;

	// set the Name
	value = row["Name"] as SoftPro.OrderTracking.Common.LookupValue;
	value.Value = "Pest Inspectors, Inc.";

	// set the lookup code
	value = row["Lookup Code"] as SoftPro.OrderTracking.Common.LookupValue;
	value.Value = "PII";

	// set other values here

	// add it to the table
	table.Rows.Add( row );

	// commit
	lookups.ApplyChanges( table );
}
In Glenwood, it should be similar except you may need to set a random Guid on the __ID column directly (it is not a "LookupValue") prior to adding it to the Rows collection. This has been fixed in Morgan so that a random Guid is created for you.

Note, too, that in Morgan the LookupValue class constructor will no longer be publicly exposed, so you will no longer be able to create objects of that type. Rather, you will edit existing values as outlined above.
Post Reply