Typed DataSet
As all of us know, we can specify the Data type when we create a DataColumn for a DataTable. This is to enforce the runtime Type-safety for the column so that only data of specified data type can be stored in the column. In the same way, in most cases, we prefer to make a DataSet itself Type-safe to protect it from runtime mismatch. Hence Typed DataSets generate classes that expose each object in the DataSet in a Type-safe manner. These classes are inherited directly from the DataSet class.
Let us look into a small example that explains a Typed DataSet.
1. Using DataSet
// Create SqlDataAdapter
SqlDataAdapter daEmp = new SqlDataAdapter("SELECT empno, empname, empaddress FROM EMPLOYEE", conn);
// Create a DataSet object
DataSet dsEmp = new DataSet();
// Fill the DataSet
daEmp.Fill(dsEmp, "EMPLOYEE");
// Print first row and first column (assuming empno is the first column)
Console.WriteLine(dsEmp.Tables["EMPLOYEE"].Rows[0][0].ToString());
// Attempt to assign a string value to the first column (will cause runtime error)
try
{
dsEmp.Tables["EMPLOYEE"].Rows[0][0] = "12345";
Console.WriteLine("Value successfully assigned (This won't be printed)");
}
catch (Exception ex)
{
Console.WriteLine("Error assigning string to integer column: " + ex.Message);
}
If we observe the above code we will get a runtime error when this code gets executed as the value assigned to the column (empno) does not take string value. Also, any misspelling of the column will generate a runtime error. And also we need to go through the hierarchy to get the final value.
2. Using Typed DataSet
// Create SqlDataAdapter
SqlDataAdapter daEmp = new SqlDataAdapter("SELECT empno, empname, empaddress FROM EMPLOYEE", conn);
// Create a strongly-typed DataSet object (assuming EmployeeDS has a table named EMPLOYEE)
EmployeeDS dsEmp = new EmployeeDS();
// Fill the DataSet
daEmp.Fill(dsEmp, "EMPLOYEE");
// Print first row and first column (assuming empno is the first column)
Console.WriteLine(dsEmp.EMPLOYEE[0].empno.ToString());
// Attempt to assign a string value to the first column (will cause compile-time error)
dsEmp.EMPLOYEE[0].empno = "12345"; // Compile-time error: cannot assign string to int
If we see the above code, a typed dataset is very much similar to a normal dataset. But the only difference is that the schema is already present for the same. Hence any mismatch in the column will generate compile-time errors rather than runtime errors as in the case of a normal dataset. Also accessing the column value is much easier than the normal dataset as the column definition will be available in the schema.
How to Generate a Typed DataSet?
Let's create typed DataSet using Visual Studio IDE.
Creating a Typed DataSet in Visual Studio
Let me explain a step-by-step procedure to create a Typed DataSet in Visual Studio.
- Open VS .Net IDE Click on File -> New -> Project and Select Console Application.
- Enter the name of the project. Say TypedDataSetTest.

- Right-click on the solution and click on Add-> Add New Item will show a dialog box.

Select DataSet from the templates pane, give the name (Say TypedDs.xsd), and click on Open. This will add a file by the name TypedDs.xsd to the solution.

- Click on the Server Explorer browse to the database and drop the table on the TypedDs.xsd file.
If we check the XML file for the same then we can see the schema for the table.

There is also a corresponding DataSet class created in the project. This DataSet class and its respective XSD files have the definitions of database columns and their types. This Data can be used in the same manner as the normal dataset to get the data.
Continue here: Working with DataSet and Its Methods in ADO.NET using C#
Pankajkumar PatelPosted Aug 12, 2019, 12:02 AM
Good one ...
SubashPosted Aug 19, 2016, 11:58 AM
Nice share
RenukaPosted Jul 23, 2012, 4:43 AM
please give the code for creating a typed dataset & tell me how to add two table in a data set??????????????
RenukaPosted Jul 23, 2012, 4:37 AM
code is good
Sutha ShaliniPosted Sep 6, 2011, 1:21 AM
i'm having one doubt. Like , i've used one dataset first and assigned connections queries etc.. Working fine. But in that same project i've used another one dataset with connections etc. in this case first dataset is connection is giving a problem like invalid datasource. why? what is the problem? plz can u help me..
Sutha ShaliniPosted Sep 6, 2011, 1:18 AM
its very nice
ravi kumarPosted Jun 1, 2009, 6:52 AM
Thank you RAmaprasad
taduri sreedharPosted Feb 19, 2009, 1:18 AM
It would be better if you provide the programatic approch
Kim SteinmetzPosted Jan 20, 2009, 9:09 AM
Thanks, Ramaprasad, I am following someone's example of code in my company, but was wondering why use typed datasets. Though, by the other comments, I'm wondering if there is more I should know. :)
Rajakiran AgirPosted Nov 3, 2008, 6:52 AM
Thanx Ramaprasad, This is verygood article to understand the difference between Typed DataSet and Dataset and How to create Typed DataSet. Enclose more information on Typed Dataset.
Prakash APosted Oct 10, 2007, 2:38 AM
THnx
Aditya KumarPosted May 14, 2007, 12:44 AM
Not enough
vinay maladkarPosted Feb 5, 2007, 12:05 AM
The procedure provided here is good. But more advantages of using typed dataset over general dataset should be provided.