-2
var category = await _companyContext.PRODUCTCATEGORIES.FirstOrDefaultAsync(p => p.ID == categoryId && (bool)p.STATE);

            category.NAME = name;
            category.WORKPLACEID = workplaceid;
            _companyContext.Update(category);

My problem is in the line with NAME

  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it/) – Orion Nov 28 '22 at 10:58

1 Answers1

2

FirstOrDefaultAsync will return either an instance or null. Therefore, you should always check the return value for null e.g.,

var category = await _companyContext.PRODUCTCATEGORIES.FirstOrDefaultAsync(p => p.ID == categoryId && (bool)p.STATE);

if(category!=NULL)
{
        category.NAME = name;
        category.WORKPLACEID = workplaceid;
        _companyContext.Update(category);
}

Without the full exception text being posted I suspect that category == NULL is true

ChrisBD
  • 9,104
  • 3
  • 22
  • 35