Open Closed

RETURNING ID OF RECENTLY ADDED ROW #8732


0
alfar_re created

Hi,

Is it possible to return the just created row? Like on the following method...I'd like to get the Id of the just created chartOfAccount as below.

protected virtual async Task Create(CreateOrEditChartOfAccountDto input)
{
    var chartOfAccount = ObjectMapper.Map<ChartOfAccount>(input);


    if (AbpSession.TenantId != null)
    {
        chartOfAccount.TenantId = (int)AbpSession.TenantId;
    }


    await _chartOfAccountRepository.InsertAsync(chartOfAccount);
}

6 Answer(s)
  • 0
    maliming created

    hi

    Try

    var id = await _chartOfAccountRepository.InsertAndGetId(chartOfAccount);
    
  • 0
    alfar_re created

    Hi,

    This is the result...

    System.NullReferenceException: Object reference not set to an instance of an object.
       at reos.ChartOfAccounts.ChartOfAccountsAppService.Create(CreateOrEditChartOfAccountDto input) in D:\OneDrive\Projects\reos\src\reos.Application\ChartOfAccounts\ChartOfAccountsAppService.cs:line 137
       at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func1 postAction, Action1 finalAction)
       at reos.ChartOfAccounts.ChartOfAccountsAppService.CreateOrEdit(CreateOrEditChartOfAccountDto input) in D:\OneDrive\Projects\reos\src\reos.Application\ChartOfAccounts\ChartOfAccountsAppService.cs:line 115
       at lambda_method(Closure , Object )
       at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
       at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
    
  • 0
    maliming created

    hi alfar_re

    Can you share the complete code of the ChartOfAccountsAppService.Create method?

  • 0
    alfar_re created

    Hi @maliming,

    Here you go

    [AbpAuthorize(AppPermissions.Pages_ChartOfAccounts_Create)]
    protected virtual async Task Create(CreateOrEditChartOfAccountDto input)
    {
        var chartOfAccount = ObjectMapper.Map<ChartOfAccount>(input);
    
    
        if (AbpSession.TenantId != null)
        {
            chartOfAccount.TenantId = (int)AbpSession.TenantId;
        }
    
        //await _chartOfAccountRepository.InsertAsync(chartOfAccount);
        var id = await _chartOfAccountRepository.InsertAndGetIdAsync(chartOfAccount);
    }
    
  • 0
    maliming created

    The method looks fine, can you debug to see which object is null?

    System.NullReferenceException: Object reference not set to an instance of an object. at reos.ChartOfAccounts.ChartOfAccountsAppService.Create(CreateOrEditChartOfAccountDto input) in D:\OneDrive\Projects\reos\src\reos.Application\ChartOfAccounts\ChartOfAccountsAppService.cs:line 137

  • 0
    alfar_re created

    Hi,

    I actually got it working. Thank you for your pointers.