This is a migrated thread and some comments may be shown as answers.

Access business entity at auto updates\inserts

3 Answers 39 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 05 Aug 2010, 04:27 AM
Hello.

I've got RadGridView with ObjectDataSource and bind IList<Domains> to grid. Try use automatic dataSource operations. I need init some field of new(updated) entity, which are not displayed at popup from(I use popup for add\update entities). But I can't interrupt save\update operations and get my entity for changing. At OnInsertCommand e.Item.DataItem is null, so how can i resolve my issue?

Thanks.

3 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 10 Aug 2010, 09:42 AM
Hello Andrew,

Please try using the following code to get the inserted item:
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)  
  {  
      GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;  
      //...
  }

For additional information, you can examine the Insert/Update/Delete at database level with queries help topic.

Sincerely yours,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Luis
Top achievements
Rank 1
answered on 06 Oct 2010, 06:01 PM

Hi,

I think Insert/Update/Delete at database level with queries help topic cant be helpful here bacause the scenario is about a BusinessLogic Layer that consumes the Data Access Layer (Domain / Entity Framework) and the SQL Queries are at data access layer and not at codebehind of the web UI like all of your examples.

Here is the scenario:
Object Data Source for RadGrid

<asp:ObjectDataSource ID="odsUsers" runat="server" 
        SelectMethod="GetAll" 
        TypeName="XXX.BusinessLogic.UserBL" 
        DeleteMethod="Remove" 
        UpdateMethod="Update"
        InsertMethod="Create"          
        DataObjectTypeName="XXX.DomainModel.User">
</asp:ObjectDataSource>

RadGrid creates the columns but just for Known Types, because it doesnt create the column for EntityKey, that it is an object type (System.Data.EntityKey), so the problem is that when we try to update, the grid use the UpdateMethod and send the current item under changes with entitykey = null and we get the error when we try to attach the entity to context and try to update because EntityKey is null.

Here is BusinessLogic for Update:
public override User Create(User entityObj)
        {
            try
            {
                using (var modelContext = new XXX_Entities())
                {
                    modelContext.AddToUserSet(entityObj); //here is the exception
                    modelContext.SaveChanges();
                    modelContext.Detach(entityObj);
                }
                return entityObj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

We try to catch the item at UpdateCommand Event but e.Item.DataItem = null, is there any solution to save a Column with an object? or any solution with entity framework binding?. We have already saw the Entity Framework examples but they connect directly with DomainContext and thats not our scenario, we are working with Disconnected Entity Framework Objects (n tiers).

And by the way, why the grid is executing the Select Method of the Object Data Source at Paging or when we select an item to edit? is there any on client paging or something?

Best Regards
-Luis
0
Mira
Telerik team
answered on 11 Oct 2010, 03:46 PM
Hello Luis,

Straight to your questions:
  1. e.Item.DataItem is available only during data-binding similar to MS DataGrid and GridView. Please take a look at the following help topics describing how to fetch the data from the edited fields in a RadGrid in the ItemCommand event handler:
    Updating values in-place and with edit forms
    Updating values using UserControl/FormTemplate 
  2. The described behavior is expected due to the event sequence of the RadGrid.

I hope this helps.

All the best,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Andrew
Top achievements
Rank 1
Answers by
Mira
Telerik team
Luis
Top achievements
Rank 1
Share this question
or