Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3998592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:32:41+00:00 2026-05-20T07:32:41+00:00

I have an issue with a Loop that doesn’t actually loop. I’ve posted a

  • 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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:32:42+00:00Added an answer on May 20, 2026 at 7:32 am
     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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue that is driving me a bit nuts: Using a UserProfileManager
We have an issue using the PEAR libraries on Windows from PHP . Pear
I have an issue with using AWK to simply remove a field from a
I have an issue with watin test that should browse for pdf and upload
Hey, so basically I have this issue, where I'm trying to put an equation
I have a slight issue with some code I'm writing if(parameter == 1) {
So I have this simple PHP loop that is generating html table data with
Basically I have a program that has 6 sliders that send a value through
We have an issue related to a Java application running under a (rather old)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.