asp.net - For Loop in C# not looping -
i have issue loop doesn't loop. i've posted simplified version of code below. basically, using npoi excel library, have excel file data on first , second sheet, need loop through both sheets.
below have done far, works through first sheet , exits. fails increment variable w. can see, there other loops implemented in code function fine don't it.
it's been long day , perhaps i'm missing simple. have placed wrong or something. if else can spot might doing wrong i'd grateful :)
public class salesfileprocessor : isalesprocessor { public list<ftpsalesrow> processsalesfile(string filename) { try { using (filestream fs = file.open(filename, filemode.open, fileaccess.read)) { int numberofsheets = 2; //loop through sheets - not work (int w = 0; w <= numberofsheets; w++) { hssfworkbook templateworkbook = new hssfworkbook(fs); hssfsheet sheet = templateworkbook.getsheetat(w); hssfrow row = null; (int = 1; <= sheet.lastrownum; i++) { ftpsalesdetails t = null; int currentcolumn = 0; try { modelcontainer ctn = new modelcontainer(); row = sheet.getrow(i); if (row == null) { continue; } t = new ftpsalesdetails { rownumber = i, invoicedate = getcellvalue(row.getcell(0)), countrysoldin = getcellvalue(row.getcell(1)), netunitssold = getcellvalue(row.getcell(2)), item = getcellvalue(row.getcell(3)), productcode = getcellvalue(row.getcell(5)), }; if (t.productcode == null && t.netunitssold == null) { return null; } int qty = int.parse(t.netunitssold); (int x = 0; x < qty; x++) { itemsale ts = new itemsale { itemid = getitemid(t.productcode), manufacturerid = getmanufacturerid("samsung"), datesold = datetime.now, }; ctn.addtoitemsales(ts); ctn.savechanges(); } } catch (indexoutofrangeexception) { } } } //end loop - 1 doesn't work } } catch (ioexception exp) { throw new ftpsalesfileprocessingexception("could not open sales data file", exp); } catch (exception exp) { throw new ftpsalesfileprocessingexception("an unknown eror occured during processing file", exp); } return null; }
if (t.productcode == null && t.netunitssold == null) { return null; }
i'm going guess being hit, causing entire function exit. if trying exit out of iteration of loop try break;
instead, or continue mike m pointed out in comments.
Comments
Post a Comment