Show / Hide Table of Contents

    IDataStoreBase.UpdateAsync(bool resetflag = true) Method

    .NET Standard 2.x

    Asynchronously updates the database with the changes made in the DataStore.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

    public Task<int> UpdateAsync(bool resetFlag = true)
    

    Parameters

    resetflag System.Boolean

    (Optional) A Boolean value specifying whether DataStore should automatically reset the update flags:

    True (default) -- To reset the flags.

    False -- Not to reset the flags.

    Returns

    Task<int>

    Returns a task that represents the asynchronous operation.

    Remarks

    You must use the constructor of DataStore or SetDataContext method to specify the database connection before the UpdateAsync method is executed.

    By default, UpdateAsync resets the update flags after successfully completing the update. However, you can prevent the flags from being reset until after you perform other validations and commit the changes. When you are satisfied with the update, call ResetUpdate to clear the flags so that items are no longer marked as modified.

    If you call the UpdateAsync method with the resetflag argument set to false and do not call ResetUpdate, the DataStore will attempt to issue the same SQL statements again the next time you call UpdateAsync.

    Test success/failure code

    It is a good practice to test the success/failure code after calling the UpdateAsync method. You can also verify the number of rows inserted, updated, and deleted by a DataStore update by examining the values of the arguments of the UpdateEnd event.

    Updating several tables in one DataStore

    If you want to update several tables in one DataStore, you can use the Modify to change the Update property of columns in each table. To preserve the status flags of the rows and columns, set the resetflag argument to false. Because the updates all occur in the same DataStore, you cannot allow the flags to be cleared until all the tables have used them. When all the updates are successfully completed and committed, you can call ResetUpdate to clear the changed flags in the DataStore.

    Updating multiple DataStores

    If you are updating multiple DataStores as part of one transaction, set the resetflag argument to false. This will prevent the DataStore from "forgetting" which rows to update in case one of the updates fails. You can roll back, try to correct the situation, and update again. Once all of the DataStores have been updated successfully, use the DataContext.Commit method to finalize the transaction and use ResetUpdate to reset the DataStore's status flags.

    Events

    UpdateAsync can trigger UpdateStart and UpdateEnd events.

    Examples

    The following code example adds a data record to the end of the DataStore and then updates the change asynchronously to the database.

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using DWNet.Data;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.IDataStoreBaseExamples
    {
        public class UpdateAsyncExample
        {
            private readonly SchoolContext _context;    
            
            public UpdateAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public async Task<int> Example1(bool resetFlag)
            {
                // Instantiates a DataStore object with datawindow: d_department.
                var datastore = new DataStore("d_department", _context);
                
                Console.WriteLine("Retrieved Rowcount: {0}", datastore.Retrieve());
                
                // Modifies the value for the budget column in the first row.
                datastore.SetItem(0, "budget", 220000.00m);
                
                // Adds a row to the end of DataStore.
                int row = datastore.AddRow();
                datastore.SetItem(row, "Departmentid", 10);
                datastore.SetItem(row, "Name", "New Department");
                datastore.SetItem(row, "Budget", 10000m);
                datastore.SetItem(row, "Startdate", DateTime.Now);
                datastore.SetItem(row, "Administrator", 2);
                
                int AffectedRow = 0;
                
                try 
                {
                    // Call asynchronous update to commit the changes to the database.
                    AffectedRow = await datastore.UpdateAsync(resetFlag);
                    
                    Console.WriteLine("After Add, Update and Retrieve, Rowcount: {0}",
                    
                    datastore.Retrieve());
                }
                catch (Exception e) 
                {
                    Console.WriteLine(e.Message);
                }
                
                return AffectedRow;
                
                /*This code produces the following output:
                
                    Retrieved Rowcount: 4
                    After Add, Update and Retrieve, Rowcount: 5           
                */
            }
        }
    }
    

    Example Refer To

    Model Class: D_Department
    DataWindow File: d_department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon