I am passing some parameters through a silverlight childwindow to a aspx page which hosts my crystal report in a pdf. It is a printing function for the stickers on file folders. I have created the crystal report to resemblr the template of the printout sheet. I have created a stored procedure that gets the information from the database to print the labels.
The main thing is, I created a skip function so the user can skip the approiate amount of labels to print on the next one. I have expanded it so the user can print just one label or multiple labels.
So I thought to use a foreach loop so one label comes in, it prints one label, multiple labels come in, it prints the multiple. Except when i run it, i get the error Collection was modified, enumeration operation might not execute. Here is the create table and skip code.
try
{
_skip = Request.QueryString["_skip"];
_Report = Request.QueryString["_Report"];
_jobnum = Request.QueryString["_jobnum"];
jobnum1 = Request.QueryString["jobnum1"];
jobnum2 = Request.QueryString["jobnum2"];
addedBy = Request.QueryString["addedBy"];
date = Request.QueryString["date"];
// Create Table
tb = new DataTable();
tb.Columns.Add("FileName", Type.GetType("System.String"));
tb.Columns.Add("PieceType", Type.GetType("System.String"));
tb.Columns.Add("PieceNumber", Type.GetType("System.String"));
tb.Columns.Add("JobNumber", Type.GetType("System.String"));
tb.Columns.Add("OpenDate", Type.GetType("System.String"));
tb.Columns.Add("Market", Type.GetType("System.String"));
tb.Columns.Add("MarketYear", Type.GetType("System.String"));
tb.Columns.Add("BusinessName", Type.GetType("System.String"));
int length;
int.TryParse(_skip, out length);
length = Convert.ToInt16(_skip);
// Populate Blank Rows to skip labels
for (int i = 0; i < length; i++)
{
dr = tb.NewRow();
tb.Rows.Add(dr);
}
//Create rows depending on how many jobs come in.
foreach (DataRow drJob in tb.Rows)
{
dr = tb.NewRow();
dr["FileName"] = drJob["PieceType"].ToString() + drJob["PieceNumber"].ToString();
dr["PieceType"] = drJob["PieceType"].ToString();
dr["PieceNumber"] = drJob["PieceNumber"].ToString();
dr["JobNumber"] = drJob["JobNumber"].ToString();
dr["OpenDate"] = drJob["OpenDate"].ToString();
dr["Market"] = drJob["Market"].ToString();
dr["MarketYear"] = drJob["MarketYear"].ToString();
dr["BusinessName"] = drJob["BusinessName"].ToString();
tb.Rows.Add(dr);
}
ShowPDF(tb);
}
catch (Exception ex)
{
}
So during the catch, it displays that error. I tried to add a .ToList() at the end of the tb.Rows but it is not supported. I search around for a bit and tried the following code
foreach (DataRow drJob in tb.Rows.Cast<DataRow>().ToList())
This part actually went through, but generated blank labels, and created labels for how many I chose to skip.(It would skip ‘9’ rows/tables and then generate ‘9’ labels for print)
My question is, how can I modify the foreach loop to 1) not have that error and 2) produce the correct amount of labels, whether its one or ten. I am guessing that modifying the length(skip) is causing the colletion was modified, but if so, how can I create a seperate list for it(.ToList()) while adjusting for the difference of parameters. They are passed in through two different ways to this same aspx btw.
This is how I would do it:
This way you’re not attempting to modify the Rows collection during the iteration, you just build up the new rows you want and add them to the collection all at once.