The Self-Taught Programmer

Recipes from real-world experience with asp.net.

How To Edit a Record in an ASPxGridView Bound to EntitySpaces’ esDataSource that Uses a Join

leave a comment »

The Situation:

You get an error when you try to save edits to a record in a DevExpress ASPxGridView. The grid view is bound to an EntitySpaces esDataSource, which is populated using EntitySpaces dynamic query functionality. The ES dynamic query syntax allows you to use joins between entities like this (actual code from one of my recent projects):

private SubscriptionScopeCollection GetScopes(int catid)
{
	SubscriptionScopeQuery ssq = new SubscriptionScopeQuery("q1");
	VwClientSubscriptionsQuery csq = new VwClientSubscriptionsQuery("q2");

	ssq.Select(ssq, csq.ClientName );
	ssq.InnerJoin(csq).On(csq.SubscriptionId == ssq.SubscriptionId);
	ssq.OrderBy(csq.ClientName.Ascending, csq.SObjectName.Ascending);

	SubscriptionScopeCollection coll = new SubscriptionScopeCollection();
	coll.Load(ssq);
	
	return coll;
}

What this gives you is an entity collection full of entities that have extra columns (the fields from the joined tables – in this case, the “ClientName” field). EntitySpaces stores these extra columns in a Dictionary object.

Why This Causes an Error…

esDataSource has functionality built in to update/delete an entity. This is one of its biggest advantages. When it tries to save the changes, it creates a new version of the entity to work with. As the EntitySpaces documentation states, you have to put the following code into your code-behind to allow it to do this:

protected void esDataSrc_esCreateEntity(object sender, esDataSourceCreateEntityEventArgs e)
{
    SubscriptionScope entity = new SubscriptionScope();
    if (e.PrimaryKeys != null)
        entity.LoadByPrimaryKey((int)e.PrimaryKeys[0]);
 
    // Assign the Entity
    e.Entity = entity;
}

However, the ASPxGridView sends ALL field values back to be saved, including the extra fields. The entity created using the code above does not contain those extra fields, so it will throw an error when it tries to process them.

The Solution:

To make the entity recognize those extra fields, you have to add them into the entity’s ExtraColumns object. Doing so will NOT allow the entity to save their data, but it will keep it from throwing an error. (If you want to save the data from those extra fields, you have to override the esDataSource’s save functionality with your own.)

Add the extra columns to the entity by adding the indicated lines to the code above

protected void esDataSrc_esCreateEntity(object sender, esDataSourceCreateEntityEventArgs e)
{
    SubscriptionScope entity = new SubscriptionScope();
    if (e.PrimaryKeys != null)
        entity.LoadByPrimaryKey((int)e.PrimaryKeys[0]);

    // THESE LINES ADDED TO MAKE THE ENTITY RECOGNIZE THE EXTRA FIELD
    Dictionary<string,object> xtra = new Dictionary<string,object>();
    xtra.Add("ClientName", null);
    entity.SetExtraColumns(xtra);
 
    // Assign the Entity
    e.Entity = entity;
}
Advertisements

Written by Jim McMullen

August 12, 2012 at 12:18 am

Posted in How-To

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: