Working With DataSet and It's Methods in ADO.NET


Introduction:

The DataSet class is a very important class of .NET Framework. It is used in disconnected architecture. It represent records in the form of Database table (Row and Column) format. It stores record of one or more tables. I this article, I am describing more useful methods of DataSet using simple programs. I am taking a DataGridView to describe DataSet's method in easier way.

At first, create a Windows Form Application. Then create a DataGridView and fill it with data through a DataSet. Look at the code given below.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
using System.Data;
 

namespace
dataset
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        string strconn = "Data Source=YourServerName;Initial Catalog=EMP;Integrated Security=True";
        private void Form1_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from userdet", strconn);
            ds = new System.Data.DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
}

Output:
 

methods of dataset
 
Now work with DataSet's Methods.

Methods:

AcceptChanges():
This method saves changes which are made with records in a DataSet.
Create a Button and add the below code in the first code (given above) on the button click event.

private
void btnAcceptChanges_Click(object sender, EventArgs e)
        {
            ds.AcceptChanges();
        }

Run the application.
Output:

copy and clone

Now, make some changes in the records of datagridview (dataset's record) and click the "AcceptChanges" button. You will note that all the changes has saved into datagridview (Note here that these changes will not save into database. Only these changes will update the record of DataSet.) I have made some changes in first row and also added last row. Look at below figure.

dataset method in ado.net

Clear(): This method clears(removes) all rows from DataSet.
Create a button, set it's text as Clear and write the below code in the click event.

private void btnclear_Click(object sender, EventArgs e)
        {
            ds.Clear();

        }

Run the application.
Output:

clear method of dataset

Click the "clear" button. You will note that all rows are clear. Like as below figure.

clear method of dataset

Clone(): The clone method copy the structure of DataSet. Means it copy only schema not full records of DataSet.
Take another DataGridView and one button in your project and write the following code on button click event.

private
void btnclone_Click(object sender, EventArgs e)
        {
            DataSet daset = ds.Clone();
            dataGridView2.DataSource = daset.Tables[0];
        }
Now run the application.
Output:

clone method of dataset

Click the "clone" button. The below figure shows that only structure has copied.

clone method of dataset

Copy(): The copy the whole records with structure of DataSet.
Take a button and set it's
text as Copy and write the following code on click event.

private
void btncopy_Click(object sender, EventArgs e)
        {
            DataSet daset = ds.Copy();
            dataGridView2.DataSource = daset.Tables[0];
        }
Now run the application.

Output:
 
copy method of dataset in ado.net

Click the copy button.

copy method of dataset

RejectChanges(): This method discard changes which is made with dataset and set the DataSet to previous stage (which was at first).
Take a button. set it's
text as RejectChanges and write the following code on button click.

private void btnRejectChanges_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
        }

Run the application.
Output: (I have made some changes in records of DataSet by modifying and adding records. The below figure shows records into datagridview after some changes made into dataset.)

rejectchanges method in ado.net

Click the
"RejectChanges" button. It will RollBack the changes with had made into dataset. Look at below figure.

 rejectchanges method in ado.net

HasChanges(): This method return boolean value to show whether record of DataSet has changed or not. It returns true if any changes has made and false if no any changes made.
Take a button and set it's text as "HasChanges" and write the following code on button click.

private void btnHasChanges_Click(object sender, EventArgs e)
        {
            if (ds.HasChanges())
            {
                MessageBox.Show("Changes Has Made");
            }
            if (!ds.HasChanges())
            {
                MessageBox.Show("No Change");
            }
        }
Run the application.
Output:


haschanges method in ado.net
Now click at
"HasChanges" button after doing some changes in dataset record.
Output:

haschanges method in ado.net

GetChanges(): This method does copy of those record, which is changed or modified.
To understand its function, take a button as "GetChanges" and write the following code on it's click event.

private
void btnGetChanges_Click(object sender, EventArgs e)
        {
            DataSet dasetGetChanges = ds.GetChanges();
            dataGridView2.DataSource = dasetGetChanges.Tables[0];
        }


Run the application.
Output:

getchanges method of dataset

Now made some changes in records of dataset and click the "GetChanges" button.

haschanges method in ado.net
 

Up Next
    Ebook Download
    View all
    Learn
    View all