So I used to write my functions in VB module and call them from any place in the project, I don't know how to do it in C#.
This is my current function in the controller
private readonly ApplicationDbContext _context; public PatRegController(ApplicationDbContext context) { _context = context; } public Int64 GetSerial() { List<FileIdSeq> NewFileSeq = new List<FileIdSeq>(); NewFileSeq = _context.FileIdSeq.FromSql("FileIdSeqSP").ToList(); var FileID = NewFileSeq[0].LastSequence; return FileID; }
and I call it
GetSerial()
Now I moved it to another static class so I can initialize it from any place as follows
namespace ARTNetCore.Functions { public static class DbSerializers { private static readonly ApplicationDbContext _context; public static Int64 FileIdSerial() { List<FileIdSeq> NewFileSeq = new List<FileIdSeq>(); NewFileSeq = _context.FileIdSeq.FromSql("FileIdSeqSP").ToList(); var FileID = NewFileSeq[0].LastSequence; return FileID; } } }
and I call it in my controller like,
[HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("FName,MName,LName,Dob,GenrId,StasId,NatyId,MarsId,CouyId,StaeId,CityId,OccnId,Email,SNN,PassNo,MobNo,LLine,MAdds,StrtNo,SDirn,AptNo,Locy,ALevl,PCode,Couy,ProeId")] PatReg patReg) { if (ModelState.IsValid) { patReg.FileId = DbSerializers.FileIdSerial(); _context.Add(patReg); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(patReg); }
But I m getting an error
An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object.
NewFileSeq = _context.FileIdSeq.FromSql("FileIdSeqSP").ToList();
what am i missing here