Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Simple Loop - Not working

I have the below code - I am basically trying to say - for each string value, execute a block of code - seems to work for the first value, but not the second...


protected void Button1_Click(object sender, EventArgs e)
    {
        string s = "27235,99282";

        string path = MapPath(".");
        path = path.Substring(0, path.LastIndexOf("\\"));
        string designerFile = path + @"\reports\APC - Claim Analysis.xlsx";

        //Create WorkbookDesigner object.
        WorkbookDesigner wd = new WorkbookDesigner();
        Workbook wb = wd.Workbook;
        WorksheetCollection sheets = wb.Worksheets;

        string[] words = s.Split(',');
        for (int i = 0; i < words.Length; i++)
        {

            wb.Worksheets.AddCopy(i);

            SqlParameter[] aryParams = new SqlParameter[4];
            aryParams[0] = new SqlParameter("@strDB", SqlDbType.VarChar);
            aryParams[0].Value = "dbRedlands";

            aryParams[1] = new SqlParameter("@intMarketGroupID", SqlDbType.Int);
            aryParams[1].Value = 1;

            aryParams[2] = new SqlParameter("@strQuery", SqlDbType.VarChar);
            aryParams[2].Value = words[i];

            aryParams[3] = new SqlParameter("@strYear", SqlDbType.VarChar);
            aryParams[3].Value = "2010";

            DataSet ds = SqlHelper.ExecuteDataset(ConnectionStrings.PDE, CommandType.StoredProcedure, "spPDE_Report_OPStatusT_SimpleGroup", aryParams);
            DataTable dt = ds.Tables[0];
            dt.TableName = "Summary";

            //Open the template file (which contains smart markers).
            wd.Open(designerFile);

            wd.ClearDataSource();
            wd.SetDataSource(dt);
            wd.Process(i, true);

            Worksheet oWorkSheet = wb.Worksheets[i];
            oWorkSheet.Name = words[i];

            i++;
        }

        string outputName = "APC - Claim Analysis.xlsx";

        OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
        saveOptions.SaveFormat = SaveFormat.Xlsx;
        wd.Workbook.Save(this.Response, outputName, ContentDisposition.Attachment, saveOptions);
        Response.End();
    }

Open in new window

0
tbaseflug
Asked:
tbaseflug
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
tbaseflugAuthor Commented:
The above works great for the first value - but never makes it to the second...
0
 
viralypatelCommented:
any specific error that you get? Mostly sounds like an issue with file lock. Check of the file is exclusively locked by any of the statements in the first iteration which is causing the second iteration to fail.
0
 
tbaseflugAuthor Commented:
No error message - when I take out  i++ - the second code is processed - when I add it back in at the end of the loop - the first is processed - so assumed it was something in my loop - which I am just not good at here
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
AndyAinscowCommented:
       for (int i = 0; i < words.Length; i++)
        {
....
            i++;   //I think this is your problem
        }


if there are 2 entries.  The first is processed (i == 0), then i is incremented (i ==1) then i is incremented again (for statement, i now equals 2) so the loop is broken out of as i is greater than the number of words.
0
 
AndyAinscowCommented:
In other words - i only has the values 0, 2, 4, 6....
0
 
viralypatelCommented:
right, now got it ... you do not need that additional  "i++" at the end of the for loop. (i.e. line 47 in the code that you pasted)
the i++ in the for statement servers the purpose.

Also, move the line "wd.Open(designerFile);" out of the loop and see if that works. With the current code it will try to open the designerFile in each iteration even when it already is.
0
 
tbaseflugAuthor Commented:
if I take out i++
I get index was out of range everywhere I use "i"
0
 
tbaseflugAuthor Commented:
Awesome - thanks!!!
0
 
ToddBeaulieuCommented:
The i++ in the for construct increments the index. That's correct.

The i++ at the end of the loop is evil. You'd never want to manipulate the indexer outside the for construct like that. If you need two indexers, use a separate variable and increment it inside the loop.
0
 
AndyAinscowCommented:
>>You'd never want to manipulate the indexer outside the for construct like that.

never !!  There can be reasons for doing so - but in this code block it does look out of place.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now