How to perform Batch Update in Sql
I have an Grid with Batch Mode.I would like to know how to send Batch of record to sql sever and perform the CRUD operation in single request.
SIGN IN To post a reply.
8 Replies
1 reply marked as answer
VN
Vignesh Natarajan
Syncfusion Team
April 21, 2021 04:25 AM UTC
Hi Ismail,
Thanks for contacting Syncfusion support.
Query: “I have an Grid with Batch Mode.I would like to know how to send Batch of record to sql sever and perform the CRUD operation in single request”
We have analyzed your query and we suggest you to achieve your requirement using OnBatchSave event of the Grid. OnBatchSave event will be triggered when all the batch changes are saved (either by clicking the Update button or calling EndEdit() method) in Grid. In the event arguments, we can find the batch changes like inserted, deleted / updated records.
Using these value we suggest you to make request to your sql server and update the changes. Refer the below code example.
|
<SfGrid DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })">
<GridEvents OnBatchSave="BatchSaveHandler" TValue="Order"></GridEvents>
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Batch"></GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
public List<Order> Orders { get; set; }
public void BatchSaveHandler(BeforeBatchSaveArgs<Order> args)
{
var ChangedRecords = args.BatchChanges.ChangedRecords;
var InsertedRecords = args.BatchChanges.AddedRecords;
var DeletedRecords = args.BatchChanges.DeletedRecords;
// Here you can customize your code
} |
Refer our UG documentation for your reference
Please get back to us if you have further queries.
Regards,
Vignesh Natarajan
Marked as answer
KI
KINS
April 21, 2021 12:49 PM UTC
Thanks for providing example.
Can you please advise how to send updated list (which is coming from grid) to database via api..
VN
Vignesh Natarajan
Syncfusion Team
April 22, 2021 03:51 AM UTC
Hi Ismail,
Thanks for the update.
Query: “Can you please advise how to send updated list (which is coming from grid) to database via api..”
We need some more details about your requirement to validate further at our end. So kindly share the following details.
- Share the details about the Grid datasource.
- How you have bound data to Grid (i.e.) using datasource property or using DataManager and adaptor?
- Share more details about your API service (WebAPI, OData, ODataV4 etc).
Above requested details will be very helpful in understanding your requirement and provide solution as early as possible.
Regards,
Vignesh Natarajan
KI
KINS
April 22, 2021 12:51 PM UTC
Please see my below points
1.I have bind data in grid using ADO.net (output is list)
2.I am going to use Web API by post method
KI
KINS
April 23, 2021 01:45 PM UTC
Awaiting for reply
VN
Vignesh Natarajan
Syncfusion Team
April 26, 2021 06:25 AM UTC
Hi Ismail,
Sorry for the delay in getting back to you.
Query: “Can you please advise how to send updated list (which is coming from grid) to database via api..”
We have analyzed your query and we have achieved your requirement by sending the entire batch changes object to the WebAPI using a intermediate service. Refer the below code example.
|
@page "/"
@inject OrderService OrderData;
@using APIService.Data
@using Syncfusion.Blazor.Grids
<SfGrid @ref="Grid" TValue="Order" DataSource="@Orders" AllowFiltering="true" Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Update", "Cancel", "Search" })" AllowSorting="true" AllowPaging="true">
<GridEvents OnBatchSave="OnSave" TValue="Order"></GridEvents>
. . . . . .
</SfGrid>
@code{
public List<Order> Orders { get; set; }
SfGrid<Order> Grid { get; set; }
protected override async Task OnInitializedAsync()
{
Orders = await OrderData.GetPeople();
}
public async Task OnSave(BeforeBatchSaveArgs<Order> Args)
{
await OrderData.UpdateBatchChanges(Args.BatchChanges);
Grid.Refresh();
}
[OrderService.cs]
public async Task<HttpResponseMessage> UpdateBatchChanges(BatchChanges<Order> value)
{
//value.OrderID = random.Next();
return await _httpClient.PostAsync($"{baseUrl}api/Default", getStringContentFromObject(value));
}
private StringContent getStringContentFromObject(object obj)
{
var serialized = JsonConvert.SerializeObject(obj);
var stringContent = new StringContent(serialized, Encoding.UTF8, "application/json");
return stringContent;
}
[DefaultController.cs]
// POST: api/Default1
[HttpPost]
public object Post([FromBody] BatchChanges<Order> value)
{
//update your changes here
foreach (var changed in value.ChangedRecords)
{
db.UpdateOrder(changed);
}
foreach (var changed in value.AddedRecords)
{
db.AddOrder(changed);
}
foreach (var changed in value.DeletedRecords)
{
db.DeleteOrder(changed.OrderID);
}
return value;
}
}
|
For your convenience we have attached the sample which can be download from below
Note: Kindly change the connectionstring in OrderContext,.cs file based on the NORTHWND.MDF file in APP_Data folder.
Please get back to us if you have further queries.
regards,
Vignesh Natarajan
KI
KINS
April 26, 2021 12:34 PM UTC
Thanks for support...
VN
Vignesh Natarajan
Syncfusion Team
April 27, 2021 04:01 AM UTC
Hi Ismail,
Thanks for the update.
We are glad to hear that you have achieved your requirement using our solution.
Please get back to us if you have further queries.
Regards,
Vignesh Natarajan
SIGN IN To post a reply.