I'm trying to update a column and delete the row that follows it in a DataTable retrieved from sql table, whenever the column equals a value and the same column in the row which follows starts with another value.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * from tbl_Copy_Journal", con);
DataSet Journals = new DataSet();
adapter.Fill(Journals, "tbl_Copy_Journal");
foreach (DataTable table in Journals.Tables)
{
for (int i = 0; i < Journals.Tables[0].Rows.Count; i++)
{
String action = Journals.Tables[0].Rows[i]["Action"].ToString();
if (action == "reading dispensed")
{
String action1= Journals.Tables[0].Rows[i+1]["Action"].ToString();
if (action1.StartsWith("Referred"))
{
String _Action = Journals.Tables[0].Rows[i]["Action"].ToString() + ", " + Journals.Tables[0].Rows[i + 1]["Action"].ToString();
Journals.Tables[0].Rows[i + 1].Delete();
Journals.Tables[0].Rows[i]["Action"] = _Action;
}
adapter.Update(Journals, "tbl_Copy_Journal");
Journals.AcceptChanges();
}
}
}
The code returns Update requires a valid UpdateCommand when passed DataRow collection with modified rows. Is there a way to delete and update rows in datatable and to fix this code. Any help is appreciated.