Hi all
What is the best method to update a data row, here what I am doing is searching the row from the datatable , then deleting the row and after modifying the row adding the row again.

But I am getting error when I am trying to delete the row the error saying that

This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.

basically I want to update a datarow in the datatable according to the condition.

Thanks
Code is as below




C# Code:
  1. DataTable dataTable = userTrackingProcess.PageDetail;
  2.          DataTable saveDataTable = GetStructure();
  3. //STURCTURE OF BOTH DATATABLE ARE SAME
  4.          string lastPageName=string.Empty;
  5.          try
  6.          {
  7.              SessionPage sessionPage = new SessionPage();
  8.              foreach (DataRow dataRow in dataTable.Rows)
  9.              {
  10.                  Browser = (string)userTrackingProcess.Browser.ToString();
  11.                  if (!(lastPageName.Equals(string.Empty)))
  12.                  {
  13.                      if (!lastPageName.Equals(dataRow["Page_URL"].ToString()))
  14.                      {
  15.                              DataRow[] row;
  16.                              row = saveDataTable.Select("Page_URL='" + lastPageName + "'");
  17.                              if (row.Length >= 0)
  18.                              {
  19.                                  saveDataTable.Rows.Remove(row[0]);
  20.                                  saveDataTable.AcceptChanges();
  21.                                  row[0]["End_Time"] = (DateTime)row[0]["End_Time"];
  22.                                  saveDataTable.ImportRow(dataRow);
  23.                              }
  24.  
  25.                              saveDataTable.ImportRow(dataRow);
  26.                        
  27.                      }
  28.                      else
  29.                      {
  30.                          DataRow[] row;
  31.                          row = saveDataTable.Select("Page_URL='" + lastPageName + "'");
  32.                          if (row.Length >= 0)
  33.                          {
  34.                              
  35.                              saveDataTable.Rows.Remove(row[0]);
  36.                              saveDataTable.AcceptChanges();
  37.                              row[0]["Total_Hit"] = (int)row[0]["Total_Hit"] + 1;
  38.                              saveDataTable.ImportRow(dataRow);
  39.                          }
  40.                      }
  41.                  }
  42.                  else
  43.                  {
  44.                            saveDataTable.ImportRow(dataRow);
  45.                  }
  46.                  lastPageName = saveDataTable.Rows[saveDataTable.Rows.Count - 1]["Page_URL"].ToString();
  47.                  saveDataTable.AcceptChanges();
  48.              }
  49.              //SAVING THE DATA IN THE DATABSE
  50.              if (saveDataTable.Rows.Count > 0)
  51.              {
  52.  
  53.              }
  54.  
  55.          }
  56.          catch
  57.          {
  58.              throw;
  59.          }