Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 16 - Dataset.rejectchanges and dataset.acceptchanges methods

Suggested Videos
Part 13 - What is SqlCommandBuilder
Part 14 - Sqlcommandbuilder update not working
Part 15 - Disconnected data access in asp.net



This is continuation to Part 15. Please watch Part 15, before proceeding.

To understand AcceptChanges() and RejectChanges() methods better, we need to understand Row States and Row Versions.

Every DataRow that is present in DataTable of a DataSet has RowState property. Please check the following MSDN link, for different values of RowState property and their description. Different DataRowVersion enumeration values and their description is also present.
http://msdn.microsoft.com/en-us/library/ww3k31w0.aspx

HasVersion() method can be used to check if a row has got a specific DataRowVersion.
DataRow.HasVersion(DataRowVersion.Original)



When we call DataAdapter.Fill() method, data is loaded into the DataSet and the RowState of all the rows will be Unchanged. When we edit a row the row state becomes Modified. If we delete a row, the row state becomes Deleted. At this point with in the DataSet, we have got Unchanged, Deleted and Modified rows. If we then invoke, DataAdapter.Update() method, based on the RowState, respective INSERT, UPDATE and DELETE commands are executed against the underlying database table and AcceptChanges() is called automatically. 

When AcceptChanges() is invoked RowState property of each DataRow changes. Added and Modified rows become Unchanged, and Deleted rows are removed.

When RejectChanges() is invoked RowState property of each DataRow changes. Added rows are removed. Modified and Deleted rows becomes Unchanged.

Both AcceptChanges() and RejectChanges() methods can be invoked at the following levels
1. At the DataSet level - When invoked at the DataSet level, they get called automatically on each DataTable with in the DataSet, and on each DataRow within each DataTable.
2. At the DataTable level - When invoked at the DataTable level, they get called automatically on each DataRow within each DataTable.
3. At the DataRow level - Gets called only for the row, on which it is invoked.

5 comments:

  1. Sir i am very thankful to you for these videos, they are priceless for me. I would request you to add videos on unit testing sir please.
    Thank you

    ReplyDelete
  2. When AcceptChanges() is invoked RowState property of each DataRow changes. Added and Modified rows become Unchanged, and Deleted rows are removed. But sir, I don't understand if we call acceptchanges() method it should reflect that changes are done rowstate property should be set to changed as it's name signifies. It's very confusing name of the method and it's working are not matching.. Please help...
    Thank u

    ReplyDelete
  3. Venket sir, pls give code for the above example. and with insertion of entire record.
    Thank You.

    ReplyDelete
  4. Yes! i think, i hv done this,
    venket sir, pls review this code,
    i create a table, within this, text box for name,totalmarks and dropdownlist for gender, and one button for add new row.
    then,
    string strInsertCommand = "insert into tblStudents(Name,Gender,TotalMarks) values(@Name,@Gender,@TotalMarks)";
    SqlCommand insertcommand = new SqlCommand(strInsertCommand, con);
    //insertcommand.Parameters.Add("@Id", SqlDbType.Int, 0, "Id");
    insertcommand.Parameters.Add("@Name", SqlDbType.NVarChar, 50, "Name");
    insertcommand.Parameters.Add("@Gender", SqlDbType.NVarChar, 20, "Gender");
    insertcommand.Parameters.Add("@TotalMarks", SqlDbType.Int, 0, "TotalMarks");


    sda.InsertCommand = insertcommand;

    then,
    protected void AddNewRow_Click(object sender, EventArgs e)
    {
    DataSet ds = (DataSet)Cache["DATASET"];
    DataRow newDataRow = ds.Tables["Students"].NewRow();
    newDataRow["ID"] = 0;
    newDataRow["Name"] = NewName.Text;
    newDataRow["Gender"] = NewGender.SelectedValue;
    newDataRow["TotalMarks"] = NewTotalMarks.Text;

    ds.Tables["Students"].Rows.Add(newDataRow);

    Response.Write("Row State: " + newDataRow.RowState.ToString());

    Cache.Insert("DATASET", ds, null, DateTime.Now.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration);

    GetDataFromCache();
    }

    and now, insertion is worked.

    ReplyDelete
  5. venket sir, in the video, you are not used acceptchanges(), and i also have felt the same which in the feed back as "Kanchan sharma" said. when we use accept changes insted of cache.insert(...) again, it should not reflect in db, when we update data to database.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.