Problem is when I add second datarow to dataset. If I remove // comments I only get 1 row add instead of 80
SqlDataAdapter indicatorsExp = new SqlDataAdapter();
string sqlExp = "SELECT * FROM BusinessApplications.tbl_WPI_Site_Indicators_Exp " +
"where Year = '" + year + "' and Month = '" + month + "'";
indicatorsExp.SelectCommand = new SqlCommand(sqlExp, conn);
SqlCommandBuilder cbexp = new SqlCommandBuilder(indicatorsExp);
indicatorsExp.InsertCommand = cbexp.GetInsertCommand();
DataSet dsExp = new DataSet();
indicatorsExp.Fill(dsExp, "explanations");
DataTable explanations = dsExp.Tables["explanations"];
//.......
foreach (ISite site in sites)
{
DataRow drexp1 = explanations.NewRow();
try
{
drexp1["PlantId"] = site.ID;
drexp1["Month"] = month;
drexp1["Year"] = year;
}
catch { }
DataRow drexp2 = explanations.NewRow();
try
{
drexp2["PlantId"] = site.ID;
drexp2["Month"] = month;
drexp2["Year"] = year;
}
catch { }
explanations.Rows.Add(drexp1);
indicatorsExp.Update(dsExp, "explanations");
// explanations.Rows.Add(drexp2);
// indicatorsExp.Update(dsExp, "explanations");
}
1 – if that
Select *is really in your code, fix it.2 – You really need to wrap the
Rows.Add()andUpdate()statements in Try/Catch as well as what you’re doing. I’d normally use one try/catch for the whole thing, and I certainly wouldn’t swallow the exceptions as you’re doing.With those said, It’s hard from your question to understand exactly what you’re doing. After making the two above fixes, I’d also change this:
to this:
With (especially) the last two changes (only call Update once, and actually handle your exceptions, even if you’re just re-throwing them) you may fix the problem. At the very least, you should get better information about what the actual problem is so it can be located and corrected.