Problem using new entity with Telerik Grid

Posts   
 
    
Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 06-Mar-2012 09:30:49   

Hi,

I am using the following code to fetch existing list from db and add new entity row to the collection. I am binding this list to my telerik grid. I am having issue when I add/edit a row especially the primary key column. when I change any field, on property change event will fire and save will be called.. I have debugged through the code and didnt find any exception.

Can you please let me know, if this is issue with LLBLGen code I have written or with Telerik grid? Your early reply will be appreciated...

public IList CreateNewEntity(IEntity2 obj, IRelationPredicateBucket filter, List<IPrefetchPathElement2> prefetchPathElement2)
        {
            var entityType = (EntityType)obj.LLBLGenProEntityTypeValue;
            using (var adapter = new DataAccessAdapter())
            {
                var entityCollection = new EntityCollection(obj.GetEntityFactory());
                adapter.FetchEntityCollection(entityCollection, filter, SetPrefetchPath(entityType, prefetchPathElement2));
                entityCollection.Add((EntityBase2)FetchNewWithValues(obj));
                BindEvents(entityCollection);
                return entityCollection.GetList();
            }
        }

    public IEntity2 FetchNewWithValues(IEntity2 obj)
        {
            using (var adapter = new DataAccessAdapter())
            {
                var factory = obj.GetEntityFactory();
                adapter.FetchNewEntity(factory, null);
                return obj;
            }
        }

        internal ReturnStatus Save(IEntity2 obj, bool refetchAfterSave)
        {
            ReturnStatus returnStatus = Util.InitializeReturnStatusToSuccess();
            try
            {
                using (var adapter = new DataAccessAdapter())
                {
                    if (!adapter.SaveEntity(obj, refetchAfterSave))
                    {
                        returnStatus = Util.SetReturnStatus("Repository.Save", Status.InsertFailed);
                    }
                }
            }
            catch (Exception exception)
            {
                returnStatus = Util.SetReturnStatus("Repository.Save", exception, Status.Failed);
            }

            return returnStatus;
        }

Regards,

Praveen.V.Nair

Walaa avatar
Walaa
Support Team
Posts: 14984
Joined: 21-Aug-2005
# Posted on: 06-Mar-2012 10:34:45   

I am binding this list to my telerik grid. I am having issue when I add/edit a row especially the primary key column. when I change any field, on property change event will fire and save will be called..

I'm not sure what the problem is exactly. Could you please describe the issue you are facing?

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 06-Mar-2012 10:40:16   

The problem is that, Telerik grid is holding on to the updated row after I change the Primary key column. When I click on on another row, its considering it as two rows selected.

For Eg. Name is primary key column in table and when you change this column, the row selection getting frozen.

I am raising this issue here because, I am not sure, if I am doing any mistake with respect to LLBLGEN code I have written.

Walaa avatar
Walaa
Support Team
Posts: 14984
Joined: 21-Aug-2005
# Posted on: 06-Mar-2012 11:10:33   

You should not be updating the PK field, by all means. PKs should not be changeable. If you have a business need to change its value, then this field is not qualified to be the PK, and instead create an artificial key to be the PK. (e.g. identity column)

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 06-Mar-2012 11:13:18   

This is composite primary key.

Forg Eg., AgencyId and AgencyTypeId form composite primary key... AgencyId is system given and AgencyTypeId will be selected by user in UI through dropdown.

Walaa avatar
Walaa
Support Team
Posts: 14984
Joined: 21-Aug-2005
# Posted on: 06-Mar-2012 11:20:29   

It's a best practice to have an artificial PK in this case. And you can still have a unique constranit on AgencyId and AgencyTypeId together.

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 06-Mar-2012 11:27:58   

ok.

Can you please tell me, why I am facing this issue? Is the primary causing issue with LLBLGEN entity?

Meanwhile, I tried what you said, and I am still facing same issue.

Walaa avatar
Walaa
Support Team
Posts: 14984
Joined: 21-Aug-2005
# Posted on: 06-Mar-2012 12:07:43   

I'm not an expert with Telerik Grid, or 3rd party controls in general. But as I see it, this can't be an LLBLGen issue, as everybody is using our framework with 3rd party controls smoothly. And it's working fine with the VS controls too.

So it's either a Telerik issue, or most probably something in your code.

Do you Save the collection whenever a field value is changed? I'm not sure when and why are using CreateNewEntity() & FetchNewWithValues()

And whether you are binding the EntityCollection directly to the Grid, or you are using a bindingSource / DataSource.

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 06-Mar-2012 12:31:26   

I am using CreateNewEntity() & FetchNewWithValues() to create a new entity in memory and add it to the existing collection in grid.

And when they change first fields in this new blank row, I will save the entity into database.

I am facing this issue only when, primary column value saved... I am guessing that this is the time on memory entity getting saved to db and becoming a db entity.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Mar-2012 20:03:14   

Updating PK values is problematic in my opinion. Anyway, you can do it with LLBLGen. Just try this in code:

var customer = new CustomerEntity("ALFKI");
var adapter = new DataAccessAdapter();
adapter.FetchEntity(customer);

customer.CustomerId = "XYZ";
adapter.SaveEntity(customer);

This of course will rise an FK VIOLATION exception, but the update query is sent.

Now, although this is possible, it's unclear how the grid manages it. For instance in the normal .net GridViews you can't edit the columns which are part of PK (DataKeyNames Grid's property).

Another thing that is unclear is how are you saving the entity or the collection because you are not using a datasource control, you are just binding the collection to the grid. So, at some place you must have a Save logic.

IMHO this is something regarded to the grid or your own code, as Walaa said. In order to help you further, please post more info, in special any relevant ASPX and code behind code that shows how are you saving the data.

Also could be helpful if you debug some grid events to see what is occurring when you update that PK value.

David Elizondo | LLBLGen Support Team