0

I have an issue with a Loop that doesn't actually loop. I've posted a simplified version of my code below. Basically, using the NPOI excel library, I have an excel file with data on the first and second sheet, so I need to do a loop to get through both sheets.

Below is what I have done so far, however this only works through the first sheet and then exits. It fails to increment the variable w. As you can see, there are other loops implemented in this code which function fine so I don't get it.

It's been a very long day and perhaps I'm missing something very simple. I could have it placed wrong or something. If anyone else can spot what I might be doing wrong I'd be very 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 - does not work
                for (int w = 0; w <= numberOfSheets; w++)
                {
                    HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs);

                    HSSFSheet sheet = templateWorkbook.GetSheetAt(w);
                    HSSFRow row = null;


                    for (int i = 1; i <= 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);


                            for (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 - the one that doesn't work
            }
        }
        catch (IOException exp)
        {
            throw new FTPSalesFileProcessingException("Could not open the Sales data file", exp);
        }
        catch (Exception exp)
        {
            throw new FTPSalesFileProcessingException("An unknown eror occured during processing the file", exp);
        }

        return null;
    }
3
  • 2
    have you tried debugging? watching i and w? :) Commented Feb 23, 2011 at 16:02
  • @Armen, I certainly did. numberOfSheets is as you'd expect, w seems to stay at zero, while i works as it should. Commented Feb 23, 2011 at 16:03
  • Did you step through this code to verify all the values are getting set as you expect? Commented Feb 23, 2011 at 16:17

4 Answers 4

4
 if (t.ProductCode == null && t.NetUnitsSold == null)
  {
      return null;
  }

I'm going to guess that this is being hit, causing your entire function to exit. If you are trying to exit out of that iteration of the for loop try a break; instead, or a continue as Mike M pointed out in the comments.

Sign up to request clarification or add additional context in comments.

3 Comments

you're right, that is exactly what's happening. I'll have to rethink it. Ideally "for (int i = 1; i <= sheet.LastRowNum; i++)" should take care of things, but for some reason, my excel sheet in questions shows that it has 388 rows, however only two are shown in the actual sheet, which is why I implemented that if. Thanks for spotting that. Will mark as correct when the time limit is up.
I'm going to also guess that if the current row is null, he might just want to continue onto the next row, not necessarily move to the next sheet. In which case you should use continue; not break;. Of course, we're both just guessing :)
@Mike M, yes perhaps I think he's got it figured out now.
1

For what you say, assuming your variables are all ok that is the loop is not empty... have you checked you're not hiting this line in the first iteration?

if (t.ProductCode == null && t.NetUnitsSold == null)
                            {
                                return null;
                            }

Comments

0

Looking at the code, the only obvious thing sticking out to me is:

HSSFSheet sheet = templateWorkbook.GetSheetAt(w);
HSSFRow row = null;

for (int i = 1; i <= sheet.LastRowNum; i++)

I would guess that sheet.LastRowNum either equals 0 or 1.

Comments

0

Maybe the indexOutOfRangeException is thrown and thats beacuse you have only one iteration, or instead of <= you sholud use <. Does the sheet numbers start with zero ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.