tbaseflug
asked on
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();
}
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.
ASKER
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In other words - i only has the values 0, 2, 4, 6....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
if I take out i++
I get index was out of range everywhere I use "i"
I get index was out of range everywhere I use "i"
ASKER
Awesome - thanks!!!
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.
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.
>>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.
never !! There can be reasons for doing so - but in this code block it does look out of place.
ASKER