I have an MVC application which searchs in a database for lists of entries. Sometimes there are so many that I can´t show them in my Browser so I want to show only the Top100 and if needed export all of them in a csv File to open them in Excel.
That works quite well in most cases but now I have a problem that there are more entries then my System can handle and I get an OutOfMemoryException.
My Problem now is that I want to show the user of my program that there are to many entries and it can´t open them in a file but my Method has an return value of FileStreamResult. How can I show the user an explanation of the error in the view/browser? Or do you know a better solution to handle the OutOfMemoryException?
Here is the Method which throws the Exetption. It happens in the fourth line "artikel.ToList();":
public FileStreamResult DownloadCSV(string agraCd, string agrCd)
{
using (var entities = new RS2_XENTESTEntities())
{
var artikel = Select(entities, agraCd, agrCd);
var artikelliste = artikel.ToList();
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
writer.Write("Artikelnummer;");
writer.Write("Kurzbezeichnung;");
writer.Write("Bezeichnung1;");
writer.Write("Status;");
writer.Write("Einheit;");
writer.Write("Notiz");
writer.WriteLine();
foreach (var order in artikelliste)
{
//Artikelnummer – Art_nr
writer.Write(order.ART_NR + ";");
//Kurzbezeichnung – Art_kbez
writer.Write(order.ART_KBEZ + ";");
//Bezeichnung 1 – Art_bez1
writer.Write(order.ART_BEZ1 + ";");
//Status – Art_status
writer.Write(order.ART_STATUS + ";");
//Einheit – Meh_cd
writer.Write(order.MEH_CD + ";");
//Notiz – Art_notiz
if (order.ART_NOTIZ != null)
{
order.ART_NOTIZ = order.ART_NOTIZ.Replace(System.Environment.NewLine, " ");
}
writer.Write(order.ART_NOTIZ);
writer.WriteLine();
}
writer.Flush();
output.Position = 0;
return File(output, "text/csv", "Export.csv");
}
}