Show validation after insert exception

3 posts, 1 answers
  1. Thomas
    Thomas  avatar
    31 posts
    Member since:
    Jun 2012

    Posted 09 Aug 2015 Link to this post


    there is already a Question "Show validation after insert exception" but no fully answers :( for me.

    So i had this problem.

    Project ( is in MVVM with DataAccess ) and RadDataGrid

    In case i add a new Record i catch the Event "RowEditEnded" to a Command with <telerik:EventBinding> in XAML and the

    EventOperationType = GridViewEditOperationType.Insert is found.

    Then i Save the Data to DB with DataAccess   context.SaveChanges() ; and DB is sending Back a Exception DuplicateKeyException

    so Commit was not done. But Record was added to the Grid. Now i try to change the Attribute (PK) which raised the violation to a valid one and try the same again. The Event is raised "RowEditEnded" as wanted but now not in "Insert" Mode  has switched from  "GridViewEditOperationType.Insert" (as it was before on the Record)  to "GridViewEditOperationType.Edit" which is different in operation then the Insert a new Record in DB.

    So Question is how could i tell the Grid not to do this automatic switch until commit is valid (without exception ) done. 

    The Record should stay in Insert Mode until Storing was valid.

    Or if there is a better solution to handling exceptions on DB side please let me know. 

    thnx br thomas







  2. Answer
    Stefan avatar
    1073 posts

    Posted 11 Aug 2015 Link to this post

    Hello Thomas ,

    Thank you for the detailed description of the issue.

    The automatic switch to which you refer is expected, since the item is already an element of the Items collection of RadGridView. The EditOperationType cannot be with "Insert" value in such case.

    As to your question, let me propose a possible resolution for the issue you are experiencing. Generally, you shouldn't rely only on the server side for validating data. For most scenarios, this would cause unnecessary calls to the database to say the least. The data should be first validated on the client side, in order to reduce the database calls, and then save the changes to the database.

    For validating on client side, RadGridView provides several approaches. You can check our documentation on Validating
    to get to know each one. Appropriate for your scenario would be the CellValidating event of RadGridView. Within the event handler you can check if an element with a given PK has already been added to the database, and if it has -  mark the input as invalid. Thus, the element would not be added to the Items collection of RadGridView and when a valid data is entered, the EditOperationType within RowEditEnded will be with "Insert" value. You can take a look at the How to get the Primary Key(s) in EF using DbContext forum thread for further reference.

    I hope this fits your requirements.

    Best Regards,
    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 Feedback Portal and vote to affect the priority of the items
  3. Thomas
    Thomas  avatar
    31 posts
    Member since:
    Jun 2012

    Posted 12 Aug 2015 in reply to Stefan Link to this post

    Hallo Stefan , thanks for fast Reply.

    I already fear in mind that the Status is switched and i have to validate or store the status ( insert / update ) in my own Objects.

    The last one i do and it works , but i will also investigate now some time to do a DB check before and avoid the PK constraint.

    But it was not a common PK violation ( this is working well ) i have a intermediate Table so   2 PK´s and because i have a Master Detail and the Problem was on Detail , 1 PK was fixed ( from Master ) but the second one must given from Users ( not direkt but via FK + Drop Down Box).

    So i will follow your advice and will try todo a sepearte validation before. 

    Thanks regards





Back to Top