Dataset Acceptchanges in SQL Server - c#

I am loading my ADO.net dataset in by select query in command object. If I clear the dataset data with Acceptchanges will it delete the data in the sql server.
If not Please let me Know how to do?

No, it will not.
You will have to make sure that each DataRow in your DataTable is marked as 'Deleted'. That is, the rowstate of each row will have to be set to Deleted.
You can do that by traversing all rows, and call the Delete method for each Row.
Then, you'll have to call the Update method of the DataAdapter, before you call AcceptChanges. You have to call 'AcceptChanges' after you've done the changes in the DB, to indicate that the dataset / datatable does not contain anymore changes that have to be persisted in the DB.
(Calling AcceptChanges will remove the DataRows that have a RowState of 'Deleted', and will change the RowState of all other DataRows to 'UnChanged'.

Related

C# OleDB Save Changes

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\"C:\\Users\\User\\Desktop\\New Microsoft Access Database.accdb\"");
OleDbDataAdapter DataAdapter = new OleDbDataAdapter("SELECT *from pinakas", connection);
DataTable pinakas_Table = new DataTable();
DataAdapter.Fill(pinakas_Table);
MessageBox.Show(pinakas_Table.Rows[1]["Name"].ToString());
OK so this line displays the name "George" of my 1st Row of field
"Name".
pinakas_Table.Rows[1]["Name"] = "John";
Now this line sets field "Name" the value "John"
pinakas_Table.AcceptChanges();
DataAdapter.Fill(pinakas_Table);
MessageBox.Show(pinakas_Table.Rows[1]["Name"].ToString());
OK now my app displays the name "John" ! That means the DataTable "pinakas_Table" got the Change.
DataAdapter.Update(pinakas_Table);
But it's never saved to my Access database.
Updating Data Sources with DataAdapters states...
Calling AcceptChanges on the DataSet, DataTable, or DataRow will cause all Original values for a DataRow to be overwritten with the Current values for the DataRow. If the field values that identify the row as unique have been modified, after calling AcceptChanges the Original values will no longer match the values in the data source. AcceptChanges is called automatically for each row during a call to the Update method of a DataAdapter.
By calling pinakas_Table.AcceptChanges(); your modified row will be treated as if its data is unchanged from the database, so DataAdapter.Update(pinakas_Table); will see no action necessary for that row. Don't call AcceptChanges(). The Update method will call AcceptChanges() for you upon success.

How to delete the rows in DataSet table and make it reflect in the database using SQL DataAdapter?

I am executing row.Delete() and dataAdapter.Update(dataset, "mytable")
I have specified delete command as well for data adapter.
But the deleted rows are not getting deleted in the database.
Am I missing anything? Insert and update are working.
I donot want to use CommandBuilder.
Have you setup you're dataAdapter.DeleteCommand? MSDN- this is the command that will be fired for each row in the DataTable that has been deleted.
Might be the cause.

Adding a new item to a DataSet, writing it to Access DB

I'm having an issue with writing back to my Access Database (.accdb) through using a DataAdapter.
Currently, I have a Fill method which fills a DataSet Table up with data. This piece of code is currently sitting within my Form_Load().
// TODO: This line of code loads data into the 'hCAliasDataSet.Topics' table. You can move, or remove it, as needed.
this.topicsTableAdapter.Fill(this.hCAliasDataSet.Topics);
Then I have an cmdAdd_Click() event, this is obviously to add a new row into the Topcis table that sits within the hCAliasDataSet.
// Create a new row, append it to Topics table.
DataRow DR;
DR = this.hCAliasDataSet.Tables["Topics"].NewRow();
this.hCAliasDataSet.Tables["Topics"].Rows.Add(DR);
Next, I've created some code to caputre the input of one of the column values.
// Capture user input
sTopicName = Interaction.InputBox("Please Enter Topic Name", "Topic Name", null, 100, 100);
// Set the Topic value for the new Row
DR["Topic"] = sTopicName;
My problem, I'm assuming is here, where I call the dataAdapter to update the Topics table. I manually check the database, and there aren't any new rows created?
// Commit the changes back to the hCAlias DataBase (hcalias.accdb).
this.topicsTableAdapter.Update(this.hCAliasDataSet.Topics);
Edit: I believe I'm needing to create an INSERT query, for my TableAdapter, would this be correct?
The adapter should generate the insert statement automatically for you behind the scenes.
Your code looks right, but it's possible that you have a constraint on one of your columns that makes it unable to save. (like a non-nullable column that you didn't specify any data for). But, you'd usually get an exception if there was a constraint that cancelled the insert.
You can try one of these alternatives, but they're basically the same thing:
this.topicsTableAdapter.Update(this.hCAliasDataSet);
this.topicsTableAdapter.Update(DR);
this.topicsTableAdapter.Insert(sTopicName); // add any other columns in here
You should call AcceptChanges in your dataset:
hCAliasDataSet.AcceptChanges();
And then commit to the database with your TableAdapter.
The Update() method just updates existing records, you'll need to use the TableAdapter Insert() method to add a new row. VC# will have created a default Insert() method for you, (which may be overloaded)... but there will be a method that will let you explicitly insert values....
For example...
this.topicsTableAdapter.Insert(int Column1, string Column2 etc etc)
This will create a new row in your database and populate it with the values you specify.

ADO.NET DBConcurrencyException - Trying to update an already deleted row

Why is ADO.NET throwng a DBConccurencyException, when I try to update a row that is already deleted by another process, instead of just ignoring the deleted row?
Is there any available option in ADO.NET to ignore this fact?
I am using SQLCommandBuilder with ConflictOption set to ConflictOption.OverwriteChanges.
You can use the DataViewRowState enumeration to select only the modified rows.
var rowsToUpdate =
dataTable.Select(null, null, DataViewRowState.ModifiedOriginal);

Strange Problem with updating Database from Dataset

Operations:
Delete in DataGridView selected row from Dataset:
FuDataSet.FuRow row = (FuDataSet.FuRow) ((DataRowView)FuBindingSource.Current).Row;
row.Delete();
To add a new Row I'm doing:
FuDataSet.FuRow row = FuDataSet.Fus.NewFuRow();
row.Someting = "Some initial Content";
row.SomethingElse = "More Initial Content";
...
FuDataSet.Fus.AddFuRow(row);
Saving user changes in current row in Dataset:
FuDataSet.FuRow row = (FuDataSet.FuRow) (((DataRowView) FuBindingSource.Current).Row);
row.Someting = someTextBox.text;
...
Save in Database:
Validate();
FuBindingSource.EndEdit();
FuTableAdapter.Update(FuDataSet.Fus); <-- Exception here
I'm using the standard DatagridView, Dataset, TableAdapter, BindingSource Scheme VS puts automaticly up after defining the database structure. There is only a single table involved and SQL Server compact 3.5 is used.
Now my problem is that I get a Concurrency Exception (DeletedRowInaccessibleException) each time I'm doing this (starting with an empty database):
Creating a new row, delete this row, save in Database, new row, save in database, delete this row, save in database <- Exception
I think that there is some synchroniszing problem between the database and the dataset.
If I'm reloading the databse after each save via FuTableAdapter.Fill(FuDataSet.Fus) the problem is gone. However, this cannot be the intention I think.
I hope someone can help me out and spot a failure in the design or explain me what may go wrong.
Thank you!
Does your table have an auto increment identity column as the primary key? If so it might not be updating the dataset table with the new value after the insert, so when you come to delete it, it cannot find the row in the database. That could explain why it works once you called the Fill() method.
You will need to somehow return the primary key on the insert so that the dataset table stays in sync with database. If you are using a store procedure to do the inserts, then primary key can be returned using an out parameter. Not sure what the best way is if you are using an SQL insert statement in the command, but you will then have to get the primary key back from the database table and assign it to the database table row.
Not sure if you are doing this after the saveing, but calling FuDataSet.AcceptChanges() will help the dataset track new changes after the database has been updated.
What you have listed there is correct. When a new row is created in the dataset table, it creates it's own ID. When you save to the database, the database table creates it's own ID as well, which in most cases will be different to the one in the dataset.
When you created the table adapter for that table, you had to supply a sql state to create the dataset table. On the advanced Options button, there is a checkbox called "Refresh the data table". Check that to have a sql statement added after the insert and update to retrieve the identity column.
If the checkbox is disabled then I am not sure what else you could, other than reload the data after each save, which will not be optimal.
Sorry I cannot be of more assistance. Best of luck

Categories