Home > Tutorial for Beginners > Dataset in C#

Dataset in C#

Must Read: ADO.net Tutorial for beginners

It is the main core feature of ado.net. Ado.net has two main components as Dataset and Data Provider. It persists data in memory which is separately from the database. By nature it is disconnected. You can perform all DML statement. You can load data in dataset from any data source like SQL Server, Oracle etc. You can create table inside the Dataset even you can define the keys and create relationship between them.  It belongs to “System.Data” namespace.

How to Declare Dataset in C#

Using system.Data;
DataSet objDS = new DataSet();
Dataset is mostly used to populate the server control like DataGrid, DataList, DropDown and datarepeater.  You can pass data in binary format means serialization is also possible using Dataset.

Before using Dataset it must be connected with some source, once you connected with the source you can populate any of the control with the filled dataset.

DataSet can be typed or un type. Typed DataSet means, you are binding your dataset with .xsd (XML) file and you have the complete details of your tables & schema in Design times where un typed dataset means you are using “system.Data.DataSet” class and you can access your table in design time.

If you wish to take the actual advantage of DataSet, go with the typed dataset. It is directly generated class from “system. Dataset” and later on you can customize properties and methods defined in your xml. To work with strongly typed dataset you must have write xsd.

Most Important Property of DataSet

  • Table:  Most Important property of your Dataset, you can come to know whether your queries return something or not. It contains the collection of tables in your dataset. It’s like a one dimensional array. You can assign any names to each tables of your dataset

 if (objDS.Tables[0].Rows.Count > 0)


           //do some action


    DataSetName:  You can give one name to your dataset, which is easy to remember the purpose of dataset. You can do this in two different way

objDS.DataSetName = "EmployeeDS";

DataSet objDS = new DataSet("EmployeeDS");

  • HasError: It is used to check the error in your DataSet.


if (objDS.HasErrors){ //do some action }


  • Relation: It is used to define the relationship between different tables. It defines the relationship on the basis of certain keys.
objDS = DataSource;


       DataRelation dtr;

       DataColumn col1 = objDS1.Tables[0].Columns["EmpID_PK"];

       DataColumn col2 = objDS1.Tables[1].Columns["EmpID_FK"];

       dtr = new DataRelation("EmployeeRelation", col1, col2);

       // add relation


DataSet Methods

  • AcceptChanges ():  It is used to save all your changes since it was loaded till last called. When you call this method in DataSet it will be applicable for all the tables in DataSet.


  • Clone (): You can create the clone of your existing Dataset. However it will copy the structure, tables their relationship and constraints but you cannot copy the data.


  • Clear (): It is used to clear all the data from DataSet including all tables.


  • Dispose ():  You can use this method to clear all the resources used by DataSet.


Must Read: ADO.net Tutorial for beginners

Post Comments Below

  1. No comments yet.
  1. No trackbacks yet.