-1

i want get last record of table with particular column value. after that i need to increment value ..here table is empty and column propery is string

     public async Task<string> GetLastBillNo()
            {
                return this.DbSet.LastOrDefault().CustomOrderNumber;
            } // getting Object reference not set to an instance of an object”

bs logic is

 long billNo = 0;

     if (!string.IsNullOrEmpty(await invoiceRepository.GetLastBillNo()))
                    {
                        billNo = long.Parse(await invoiceRepository.GetLastBillNo());
                        billNo = billNo + 1;
                        order.CustomOrderNumber = billNo.ToString();
                    }
                    else
                        order.CustomOrderNumber = (billNo + 1).ToString();
user2564537
  • 1,041
  • 1
  • 12
  • 16

3 Answers3

3

You need to check the null Object reference before incrementing the next value.

        public async Task<string> GetLastBillNo()
        {
            return (this.DbSet.LastOrDefault() != null ? this.DbSet.LastOrDefault().CustomOrderNumber : string.Empty);
        }
awais
  • 492
  • 4
  • 17
0

Is order class static ? Else, You need to instantiate the order object. It does not exist inside the context.

TropicalViking
  • 407
  • 2
  • 9
  • 25
0
using (var order = new YourDBContext()) {
        if (!string.IsNullOrEmpty(await invoiceRepository.GetLastBillNo()))
                {
                    billNo = long.Parse(await invoiceRepository.GetLastBillNo());
                    billNo = billNo + 1;
                    order.CustomOrderNumber = billNo.ToString();
                }
                else
                    order.CustomOrderNumber = (billNo + 1).ToString();
...rest of your code
    }
TropicalViking
  • 407
  • 2
  • 9
  • 25