c# - MVC OutOfMemoryException-Handling -


i have mvc application searchs in database lists of entries. there many can´t show them in browser want show top100 , if needed export of them in csv file open them in excel.

that works quite in cases have problem there more entries system can handle , outofmemoryexception.

my problem want show user of program there many entries , can´t open them in file method has return value of filestreamresult. how can show user explanation of error in view/browser? or know better solution handle outofmemoryexception?

here method throws exetption. happens in 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");         }     } 

edit: oh didn't saw output memorystream... maybe can return kind of (http)response can write output.

your oom exception raise @ line

var artikelliste = artikel.tolist(); 

... because .tolist() call repository , each object memory. dont know select method assume, returns ienumerable or kind of reader? can use reader or ienumerable (dont call tolist()) , work (depending on select implementation).


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -