Prevent Addition of Duplicate Records

Hi All,

I have several fields in my Database that have to have UNIQUE values (excluding the Key fields).

I cannot locate an annotation for a Unique field, but came across this Forum post:


This is for .NET Core, and I was wondering if it is possible in Blazor?

I did chat with one of your support people, who suggested using "OnActionBegin" and checking for the "Save" request. However, when I click on the "Update" button in the grid, it does not seem to enter the "OnActionBegin" method (tried with 'Console.WriteLine' and also a break point in Visual Studio),

Thanks in advance for any help

Simon

9 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team September 3, 2020 01:11 PM UTC

Hi Simon, 

Greetings from Syncfusion. 

Query: Prevent Addition of Duplicate Records 

We have validated your query and you want prevent the addition of duplicate records while adding the record. You can achieve your requirement by using OnActionBegin event. Here, we have checked the added primary key value in database records. If the primary key value(OrderID) is already present in database, then we have prevent default action using Args.Cancel. So the added form will be in edit mode.  

Also, the OnActionBegin event is triggered correctly. Find the below code snippets and sample for your reference. 

 
    <SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" AllowSorting="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update"})"> 
        <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>         
        <GridEvents OnActionBegin="ActionBeginHandler" TValue="Order"></GridEvents> 
        <GridPageSettings PageSize="5"></GridPageSettings> 
        <GridColumns> 
            <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
            . . . 
        </GridColumns> 
    </SfGrid> 
 
@code{  
    public IQueryable<Order> Orders { get; set; } 
    public IQueryable<Order> NewOrders { get; set; } 
    SfGrid<Order> Grid; 
    OrderDataAccessLayer db = new OrderDataAccessLayer(); 
    protected override void OnInitialized() 
    { 
        Orders = db.GetAllOrders().AsQueryable(); 
 
    } 
    public void ActionBeginHandler(ActionEventArgs<Order> Args) 
    { 
        if(Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "Add")    
        { 
            var IsPresent = Orders.Where(or => or.OrderID == Args.Data.OrderID).FirstOrDefault(); 
            if (IsPresent != null) 
            { 
                Args.Cancel = true;   //if the same order id field exist then here we have cancelled default saving action. It will stay in edit mode 
            } else 
            { 
                db.AddOrder(Args.Data); // insert the record into your datasource 
            } 
        } 
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Delete) 
        { 
            var id = Args.Data.OrderID; 
            db.DeleteOrder(id); // delete the record        } 
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "edit") 
        { 
            db.UpdateOrder(Args.Data as Order); // update the record into your datasource 
        } 
    } 
    } 


 
Please let us know if you have any concerns. 

Regards, 
Rahul 


Marked as answer

SI Simon September 18, 2020 11:20 AM UTC

Hi Rahul,

Thanks for the reply.

All working fine now.

Regards

Simon


RN Rahul Narayanasamy Syncfusion Team September 21, 2020 04:48 AM UTC

Hi Simon, 
 
Thanks for the update. 
 
We are happy to hear that you have achieved your requirement.  
 
Please get back to us if you need further assistance. 
 
Regards, 
Rahul 



KI KINS October 4, 2021 09:04 AM UTC

I need to handle duplicate record in "Batch Mode".Please share the example.


Note:-

I need to show "Messagebox" when adding duplicate row.





RN Rahul Narayanasamy Syncfusion Team October 5, 2021 02:52 PM UTC

Hi Ahmed, 

Greetings from Syncfusion. 

Query: I need to handle duplicate record in "Batch Mode".Please share the example. Note:- I need to show "Messagebox" when adding duplicate row. 

We have validated your query and we have prepared a simple sample based on your requirement. Here we have checked the added cell value with the Grid datasource while adding the primary key cell value and shown the duplicate add message box while saving the cell value using OnCellSave event of the Grid. Find the below code snippets and sample for your reference. 

 
<SfGrid DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch"></GridEditSettings> 
    <GridEvents OnCellSave="CellSaveHandler" TValue="Order"></GridEvents> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules { Required = true })" Type="ColumnType.Number" Width="120"></GridColumn> 
        . ..  
    </GridColumns> 
</SfGrid> 
 
 
<SfDialog Width="250px" IsModal="true" @bind-Visible="@IsVisible"> 
    <DialogEvents OnOverlayClick="@OnOverlayclick"> 
    </DialogEvents> 
    <DialogTemplates> 
        <Content> The entered id is already present. Please enter new unique id </Content> 
    </DialogTemplates> 
</SfDialog> 
 
@code{ 
    public List<Order> Orders { get; set; } 
 
    . . . 
    public void CellSaveHandler(CellSaveArgs<Order> args) 
    { 
        if(args.ColumnName == "OrderID") 
        { 
            var IsPresent = Orders.Where(or => or.OrderID == (int?)args.Value).FirstOrDefault(); 
            if (IsPresent != null) 
            { 
                args.Cancel = true; 
                OpenDialog(); 
            } else 
            { 
                // insert the record into your datasource 
            } 
        } 
    } 
 
    private bool IsVisible { get; set; } = false; 
 
    private void OpenDialog() 
    { 
        this.IsVisible = true; 
    } 
 
    private void OnOverlayclick(MouseEventArgs arg) 
    { 
        this.IsVisible = false; 
    } 
} 


Reference: 

Please let us know if you have any concerns. 

Regards, 
Rahul 



KI KINS November 24, 2021 11:56 AM UTC

sorry for late reply..

I have another issue..

When I add newly row for duplicate record checking, then its mot working.

please check my comments in below screencast..


https://www.screencast.com/t/tTcHdhPxsi




RN Rahul Narayanasamy Syncfusion Team November 25, 2021 09:39 AM UTC

Hi Ahmed, 

Thanks for the update. 

We have validated your query and we are able to reproduce the reported case at our end. In the previously shared sample we have added logic to prevent the add operation by checking the Grid data source only and not checked the added records. Now we have modified the sample(checked the added records also) to work based on your case. Find the below code snippets and sample for your reference. 

 
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch"></GridEditSettings> 
    <GridEvents OnCellSave="CellSaveHandler" TValue="Order"></GridEvents> 
    <GridColumns> 
        . ..  
    </GridColumns> 
</SfGrid> 
 
 
<SfDialog Width="250px" IsModal="true" @bind-Visible="@IsVisible"> 
    . ..  
</SfDialog> 
 
@code{ 
    SfGrid<Order> Grid; 
    public List<Order> Orders { get; set; } 
 
    . . . 
    public async Task CellSaveHandler(CellSaveArgs<Order> args) 
    { 
        var changes = await Grid.GetBatchChangesAsync(); 
        if(args.ColumnName == "OrderID") 
        { 
            var IsPresent = Orders.Where(or => or.OrderID == (int?)args.Value).FirstOrDefault(); 
            if (IsPresent != null) 
            { 
                args.Cancel = true; 
                OpenDialog(); 
            } else if (changes.AddedRecords.Count > 1) 
            { 
                var added = changes.AddedRecords.Select(x => x.OrderID).ToList(); 
                added.RemoveAt(0); 
                if (added.Contains((int?)args.Value)) 
                { 
                    args.Cancel = true; 
                    OpenDialog(); 
                } 
            } 
            else 
            { 
                // insert the record into your datasource 
            } 
        } 
    } 
} 


Please let us know if you have any concerns. 

Regards, 
Rahul 



KI KINS December 2, 2021 10:21 AM UTC

Thanks for reply..

above code working fine but when I press "Escape" key, then its not working (its accepting duplicate value).please check this and advise how to resolve this issu..



RN Rahul Narayanasamy Syncfusion Team December 3, 2021 12:23 PM UTC

Hi KINS, 

Thanks for the update. 

We have validated your query and we would like to inform you that while pressing the Esc key in edit mode it will cancel the edit state. In this case the validation will not performed while pressing the Esc key. 

Also while pressing the Esc key, it will close the edit state of the cell. It will not save the entered value in that cell. 

Please let us know if you have any concerns. 

Regards, 
Rahul 
 


Loader.
Up arrow icon