Free Training
C Language   |   CSS   |   MainFrame   |   VBScript   |   PHP   |   XML   |   C++ Tutorials   |   Ajax   |   JavaScript   |   CSS3   |   UML   |   jQuery   |   Microsoft AJAX

Sponsored Links

ASP.NET Tutorials

 
Home Tutorials ASP.NET
 

Managing Data with ADO.NET DataSets and C#

 

Managing Data with ADO.NET DataSets and C#

Page 1 of 2

This tutorial explains about The Role of DataSets in ADO.NET , Using DataSets in ASP.NET , Saving DataSets in Session State, Using DataTable Constraints, DataSet, DataRelations Using DataSets with DataAdapters to Modify Data and the Transactional Model in DataSets.



 


ASP.NET: Managing Data with ADO.NET DataSets and C#

 


Introduction

In this article we will explore the DataSet, DataTable and DataRelations classes. Those who have worked with classic asp will be familiar with record sets. Record set was the individual rows that were retrieved from the database and than binded to the screen using html table or any other html tags.

DataSet on the other hand is a new technology in Asp.net and this maps the entire database maps in its self. What I mean is it can have all the fields of the tables which we specify. In other words its a disconnected database. By disconnected I mean that if you make changes in Dataset those changes will not be reflected in the actual database.


The Role of DataSets in ADO.NET

The important of datasets can never be denied nor it can be questioned. DataSet is a mirror image of the table or the query which you run. DataSets makes it easier to edit and update the information. Another good thing is that datasets are disconnected in nature so, if you make any changes in the dataset it will not reflect in the database unless use special methods to perform the change and confirm it.


Using DataSets in ASP.NET

Let's see how we can use datasets in Asp.net. The most common way of using the DataSet control is with the dataadapter. The DataAdapter fills the dataset control with data coming from the database. Let's see how we can perform this simple action:

SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROm Categories",myConnection);

DataSet ds = new DataSet();

ad.Fill(ds,"Categories");

DataGrid1.DataSource = ds;

DataGrid1.DataBind();


Explanation of the code:

1. First we declare a simple dataadapter instance which fetches all the rows from the Categories table. ( Categories table is present in the Northwind database which is shipped with Sql Server 2000 ).

2. New we created an instance of the DataSet control.

3. Next we fill the dataset with the data coming from the dataadapter control. As you can see that I have written ad.Fill(ds,"Categories"); you can also write ad.Fill(ds); and this will work fine too. In the former case I was telling the dataadapter which table I am using in my query and later the dataadapter was finding the table itself.

4. Finally I used DataGrid to bind the data on the screen.



Iterating through the DataSet:

Here is a simple code to iterate through the dataset and select individual items instead of selecting all the data in the DataSet.

SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROm Categories",myConnection);

DataSet ds = new DataSet();

ad.Fill(ds,"Categories");

DataTable dt = ds.Tables["Categories"];


Response.Write(dt.Rows[0][1].ToString()) ;

1) First few lines are identical which we have done before.

2) In the forth line we declared the DataTable object. DataTable object is used to hold a single table. As I already told you that dataset can hold multiple tables depending upon the query so we can assign a single table from the dataset to the datatable object. I have done that using the line.

DataTable dt = ds.Tables["Categories"];

3) Next we are printing the first row of the second column. Always remember that the number of the rows and columns in the database does not start with 1 but it starts at 0. So in this line of code:

Response.Write(dt.Rows[0][1].ToString()) ;

This line of code means that get Row '0' and Column 1 of the from the datatable. And so we get the value and it prints out "Beverages".


Saving dataSets in Session State:

DataSets can also be saved in the Session State so that it can be available in other areas of the application and in new pages. The way of storing the DataSet in Session State is very easy.

Session["MyDataSet"] = DataSet;

Later if you want to retrieve the DataSet from the Session State you can easily do this by using this line of code:

DataSet ds = (DataSet) Session["MyDataSet"]

As you can see that when I am retrieving the values from the Session object I am casting it into the dataset object. This is because this is explicit conversion and requires casting. I am unboxing in this case.



Next Page: Managing Data with ADO.NET DataSets and C# - Page 2


Read Next: Creating and consuming XML Web Services with C#



 

 

Comments


mr.mukesh said:

  how do you connect to Excel file using C#
January 9, 2009, 1:10 am

somanath nayak said:

  Pretty much helpful for the beginners. Thank you.
March 5, 2009, 12:43 am

Bhoopendra Nath said:

  How can we get the data in the front end regarding to the value of a selected value from the database using dataset and data adapter.
June 4, 2009, 4:26 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links