0

I am iterating through a DataView. If I don't want a row based on a predefined condition, I am deleting it. After I iterate through the entire DataView, I want to bind this updated DataView back to a control but not commit the changes to the SQL Server Database table. Will this automatically trigger the deletes in the actual sql server table? If so, how can I stop that from happening.

 foreach (DataRowView dR in dvUpdatedRankings)
 {
     // If some logic is true, delete row
     if(DeleteFlag == True)
     {
         dR.Delete();
     }

 }
Roni R
  • 3
  • 2
  • You won't be able to delete from inside the loop as you're using `dR` as the object selected from your `dvUpdatedRankings`. You will need to add them to a list of `DataRowView` objects, and then iterate over that afterwards to delete them from `dvUpdatedRankings` – Alex May 30 '18 at 17:05
  • 1
    How is DataView linked to sql server? – Samuel A C May 30 '18 at 17:09
  • Thanks Alex. Once they are deleted properly from the DataView, will they automatically be deleted from the SQL database table? That's more my concern. I just want to update the DataView and not effect the actual SQL Table. – Roni R May 30 '18 at 17:10
  • @SamuelAC I am using a SqlDataAdapter to fill a DataSet and then create a DataView from the DataSet. – Roni R May 30 '18 at 17:18

2 Answers2

2

The dataview is no way related to sql server db..It serves just as holder to save the value fetched from db,, any modification done to it will only reside in servers memory till the scope of the object is lost. Hence db would not be changed.

It is like a one way communication.. you got the data from db to DataView. and the connection is closed and now db and Dataview are two seperate things!

Samuel A C
  • 406
  • 3
  • 16
0

After deleting the rows from the dataset, call dataset.Acceptchanges(). That will reremoved the rows from the dataset that are marked as "to_be_deleted".

Ido
  • 1