Im trying to upload an excel sheet and then copy the contents of the excel file to a memory stream, finally to insert into sql server.
public async Task<List<Transaction>> Import(IFormFile file)
{
var list = new List<Transaction>();
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
using (var package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet= package.Workbook.Worksheets[0];
var rowcount = worksheet.Dimension.Rows;
for(int row=2; row<rowcount; row++)
{
list.Add(new Transaction
{
TransactionId = (int)worksheet.Cells[row, 1].Value,
AccountNumber = worksheet.Cells[row,2].Value.ToString().Trim(),
BeneficiaryName = worksheet.Cells[row, 3].Value.ToString().Trim(),
BankName = (Bank)worksheet.Cells[row, 4].Value,
SWIFTCode = worksheet.Cells[row, 5].Value.ToString(),
Amount = (int)worksheet.Cells[row, 6].Value,
});
}
}
}
return list;
}
The error occurs here where it gives a NullReferenceException: Object reference not set to an instance of an object.
await file.CopyToAsync(stream);
There arent really any answers to this from what ive searched.