Results 1 to 6 of 6

Thread: [2.0] Update a datarow

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question [2.0] Update a datarow

    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.          }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Update a datarow

    First of all, you're not deleting the row; you're removing it. Deleting a DataRow and removing it are two different things.

    Second, why are you removing it and add it back if all you want to do is update it? If all you want to do is update it then that's all you should do.

    Third, if you really must remove the row for some reason then the very error message you posted is already telling you what to do:
    This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.
    Do you want to create new data in that row? If so then do what it says and call BeginEdit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Update a datarow

    Simplly say that I want to search a data row in the datatable and if found then I want to update it.

    Thanks for the reply

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Update a datarow

    If you want to set a field in a row then just do so. You don't have to remove it first. In fact, as you've seen, removing the row discards all the data it contains.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Update a datarow

    C# Code:
    1. DataRow[] row;
    2.  row = saveDataTable.Select("Page_URL='" + lastPageName + "'");
    3.                          if (row.Length >= 0)
    4.                          {
    5.                              saveDataTable.Rows.Remove(row[0]);
    6.                              saveDataTable.AcceptChanges();
    7.                              row[0].BeginEdit();
    8.                              row[0]["Total_Hit"] = row[0]["Total_Hit"].ToString() + 1;
    9.                              saveDataTable.ImportRow(dataRow);
    10.                              row[0].EndEdit();
    11.                          }

    Above code is working for me , but is there is any direct way to update the datarow.. I am removing the datarow and adding it again.
    Thanks

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Update a datarow

    Consider this. You have an egg carton with an egg in it. You take the egg out, paint it then put it back. Now you say "I want to paint the egg again but this time without taking it out of the carton and putting it back again". What would you do? Wouldn't you just paint the egg in the egg carton without removing it first and putting it back again afterwards? Would you even have to ask?

    Now, you've got a DataTable with a DataRow in it. You take the DataRow out, edit it and then put it back. Now you say "I want to edit the DataRow but this time without taking it out of the DataTable and putting it back again". What do you do?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width