Base solution for your next web application
Open Closed

RETURNING ID OF RECENTLY ADDED ROW #8732


User avatar
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);
}
Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    Try

    var id = await _chartOfAccountRepository.InsertAndGetId(chartOfAccount);
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    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.&lt;InvokeNextActionFilterAsync&gt;g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    maliming created
    Support Team

    hi alfar_re

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

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    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);
    }
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    maliming created
    Support Team

    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

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    alfar_re created

    Hi,

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

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)