We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Blazor Grid - using SQL Stored Procedures for CRUD operations...

Can I configure a CustomAdaptor to perform CRUD operations with a Blazor Grid component using SQL Stored procedures (or do I even need to use a CustomAdaptor...)?

Using the following I can populate the Grid with data,

<EjsGrid DataSource="@dataApps" AllowSorting="true" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })">
     <GridColumns>
          <GridColumn Field=@nameof(HebronAppModel.Description) HeaderText="Description" Width="150"></GridColumn>
     </GridColumns>
</EjsGrid>

@code{
     IEnumerable<DataAppModel> dataApps;

    protected override async Task OnInitializedAsync()
    {
       dataAapps = await DataAppService.DataAppList();
    }
}

Attempts to use "<EjsDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></EjsDataManager>" and modifying/adopting from existing Blazor documentation has unfortunately failed..!

5 Replies

VN Vignesh Natarajan Syncfusion Team January 15, 2020 03:41 AM UTC

 Hi Kim,  
 
Greetings from Syncfusion support.  
 
Query: ” Can I configure a CustomAdaptor to perform CRUD operations with a Blazor Grid component using SQL Stored procedures  
 
Using CustomAdaptor feature we can customize the default actions like databinding, CRUD and data operations on our own using the Read / ReadAsync (used for data binding / data opertions) method of Custom Adaptor. Similarly for CRUD operations we have provided below methods.  
 
  1. Insert/InsertAsync
  2. Remove/RemoveAsync
  3. Update/UpdateAsync
  4. BatchUpdate/BatchUpdateAsync – Batch editing
 
In above methods, we will pass the modified data / inserted records details as an arguments. From there you can achieve you requirement. We have already documented the details about this feature in our UG documentation. Kindly refer the below link for your reference 
 
 
 
Kindly refer our online demo for your reference 
 
 
Please get back to us if you have further queries.    
 
Regards, 
Vignesh Natarajan 



KS Kim Shadbolt January 20, 2020 07:29 AM UTC

Hi Vignesh,

Thank you for your advises. I am still unable to correctly configure the data binding for the Grid - despite much effort!

My setup includes a SQL db back-end, using Dapper with stored procedures - which all work fine from Blazor when using Dependency Injection and HTML table and form for CRUD operations. For differing reasons, Entity Framework with controllers and API is not being used at this time.

Using the documentation for data binding and editing, the closest I've been able to achieve to error free code (for a start!) is the following page (code). Although it is not populating data yet, I am hopeful that you may be able to comment on the setup...

Thank you for your patience and time, I am sincerely trying to learn and understand the process for myself.

@using Syncfusion.EJ2.Blazor
@using Syncfusion.EJ2.Blazor.Grids
@using Syncfusion.EJ2.Blazor.Data
@using Newtonsoft.Json;
@using HebronDataAccess.Models.Administration.Development


@inject IHebronAppService  HebronAppService
@inject NavigationManager NavigationManager

<div class="col-lg-12 control-section">
    <div class="content-wrapper">
        <div class="row">
            <EjsGrid TValue="HebronAppModel" AllowPaging="true" Toolbar="@(new string[] {"Add","Edit","Delete","Update","Cancel"})">
                <EjsDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></EjsDataManager>
                <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>
                <GridColumns>
                    <GridColumn Field=@nameof(HebronAppModel.Id) HeaderText="App Id" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
                    <GridColumn Field=@nameof(HebronAppModel.AppName) HeaderText="App Name" TextAlign="TextAlign.Right" Width="120"></GridColumn>
                    <GridColumn Field=@nameof(HebronAppModel.Description) HeaderText="Customer Name" Width="150"></GridColumn>
                    <GridColumn Field=@nameof(HebronAppModel.ParentId) HeaderText="ParentId" Format="d" Type="ColumnType.Number" TextAlign="TextAlign.Right" Width="130"></GridColumn>
                    <GridColumn Field=@nameof(HebronAppModel.AppImageUrl) HeaderText="App Image Url" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>

                </GridColumns>
            </EjsGrid>
        </div>
    </div>
</div>
@code{

    public class CustomAdaptor : DataAdaptor
    {
        HebronAppService appService = new HebronAppService();

        IEnumerable<HebronAppModel> hebronapp;

        public override async Task<Object> ReadAsync(DataManagerRequest dataManagerRequest, string key = null)
        {

            hebronapp = await appService.HebronAppList();
            IEnumerable<HebronAppModel> GridData = hebronapp;
            if (dataManagerRequest.Skip != 0)
            {
                GridData = GridData.Skip(dataManagerRequest.Skip); //Paging
            }
            if (dataManagerRequest.Take != 0)
            {
                GridData = GridData.Take(dataManagerRequest.Take);
            }
            return dataManagerRequest.RequiresCounts ? new DataResult() { Result = GridData } : (object)GridData;
        }
        public override async Task<Object> InsertAsync(DataManager dataManager, object value, string key)
        {
           
            var val = (value as HebronAppModel);
           
            await appService.HebronAppsInsert(val);
            return value;
        }
        public override async Task<object> RemoveAsync(DataManager dataManager, object value, string keyField, string key)
        {

            int data = (int)value;
           
            await appService.HebronAppsDelete(data);
            return value;
        }
        public override async Task<object> UpdateAsync(DataManager dataManager, object value, string keyField, string key)
        {

            var val = (value as HebronAppModel);
            var data = hebronapp.Where((HebronAppModel) => HebronAppModel.Id == val.Id).FirstOrDefault();
            if (data != null)
            {
                data.Id = val.Id;
                data.AppName = val.AppName;
                data.Description = val.Description;
                data.AppImageUrl = val.AppImageUrl;
                data.ParentId = val.ParentId;
            }
            await appService.HebronAppsUpdate(val);
            return value;
        }
    }

}




VN Vignesh Natarajan Syncfusion Team January 21, 2020 08:21 AM UTC

Hi Kim,  
 
Thanks for the update.  
 
Query: “ Although it is not populating data yet, I am hopeful that you may be able to comment on the setup... 
 
We are able to reproduce the reported issue at our end only when we try to bind the synchronous data in Asynchronous method. (i.e.) while using ReadAsync method, at-least one asynchronous operation must be handled and awaited for result, we suspect in your case, no asynchronous operation is happening. So kindly ensure that your data fetching process in asynchronous one.  
 
Also share logic behing appService.HebronAppList(); method to check whether it is asynchronous in your case. 
 
If you still want to use ReadAsync method then we suggest you to modify the code like below.  
 
public override async Task<object> ReadAsync(DataManagerRequest dataManagerRequest, string key = null) 
        { 
            hebronapp = await Task.Run(() => appService.GetAllOrders()); 
            IEnumerable<Order> GridData = hebronapp; 
            if (dataManagerRequest.Skip != 0) 
            { 
                GridData = GridData.Skip(dataManagerRequest.Skip); //Paging 
            } 
            if (dataManagerRequest.Take != 0) 
            { 
                GridData = GridData.Take(dataManagerRequest.Take); 
            } 
            return dataManagerRequest.RequiresCounts ? new DataResult() { Result = GridData, Count = hebronapp.Count() } : (object)GridData; 
        } 
 
Note: Also ensure to return the Count while returning the value the datasource which will be used by pager to display appropriate total pages and item count. Refer our UG documentation for your reference 
 
 
If you are still facing the issue even when your process in asynchronous, kindly get back to us with issue reproducible sample.   
 
Regards, 
Vignesh Natarajan 
 



KS Kim Shadbolt January 23, 2020 09:45 AM UTC

Hi Vignesh,

Found that the connection string was not being returned because of wrong reference to the HebronAppService.

Replaced "HebronAppService appService = new HebronAppService" with

IHebronAppService appService;

        public CustomAdaptor(IHebronAppService appService)
        {
            this.appService = appService;
        }

and added the missing "services.AddScoped<CustomAdaptor>();" to startup.cs.

All working good now, thanks!


VN Vignesh Natarajan Syncfusion Team January 24, 2020 04:06 AM UTC

Hi Kim,  
 
Thanks for the update.  
 
We are glad to hear that you have resolved your query.  
 
Kindly get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan. 


Loader.
Live Chat Icon For mobile
Up arrow icon