Reputation: 5535
I have the following code
private void bgwSendMail_DoWork(object sender, DoWorkEventArgs e)
{
DataSet ds = getMailToSend();
DataTable table = ds.Tables[0];
{
foreach (DataRow row in table.Rows)
{
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
string attachment2 = ds.Tables[0].Rows[0]["Attachment2"].ToString();
string attachment3 = ds.Tables[0].Rows[0]["Attachment3"].ToString();
string attachment4 = ds.Tables[0].Rows[0]["Attachment4"].ToString();
string mailTo = ds.Tables[0].Rows[0]["EmailTo"].ToString();
string mailSubject = ds.Tables[0].Rows[0]["EmailSubject"].ToString();
string mailBody= ds.Tables[0].Rows[0]["EmailBody"].ToString();
string uid = ds.Tables[0].Rows[0]["uid"].ToString();
if (String.IsNullOrEmpty(attachment1))
{
//TODO Send Email Straight away ignore rest
}
else
{
if (!String.IsNullOrEmpty(attachment1))
{
bool attachment1Exists = checkFileExists(attachment1);
if (attachment1Exists == false)
{
continue;
}
}
Now I would expect, when we hit continue (which does get hit) at the bottom, that we should exit back up to the foreach as below and move on to the next record in the dataset
This does not happen, it iterates over the same record from which the continue came from over and over, is this normal?
If it's normal what's the best way to get the foreach
to ignore that row in the datatable once it's been exited once?
Upvotes: 0
Views: 119
Reputation: 128
Change
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
to
string attachment1 = row["Attachment1"].ToString();
and all other subsequent references to the DataRow.
Upvotes: 4
Reputation: 460208
The continue
is working as expected.
You are enumerating all rows in the table but you aren't using it. Instead you are always accessing the first row in the table:
DataTable table = ds.Tables[0];
foreach(DataRow row in table.Rows)
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
// ...
}
You are always accessing ds.Tables[0].Rows[0]
.
Instead you should use this code:
foreach(DataRow row in table.Rows)
{
string attachment1 = row["Attachment1"].ToString();
// ...
}
So you are actually enumerating all rows in the table as expected, it's not an infinite loop, but you are not using every row in the table but only the first.
Upvotes: 6