Working with disconnected architecture in ASP.NET

Working with DataTable

DataTable object in ADO.NET works in disconnected architecture. This class is available in System.Data namespace. For creating the table, first creates the table columns and rows and adds it to the table.

using System;
using System.Data;
public partial class Default3 : System.Web.UI.Page
{
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        dt = new DataTable();
        // Creating columns
        dt.Columns.Add("EmployeeID", typeof(int));
        dt.Columns.Add("Name", typeof(string));        
        dt.Columns.Add("City", typeof(string));
        dt.Columns.Add("Salary", typeof(double));
        dt.Columns.Add("Department", typeof(string));
        //Adding Rows
        dt.Rows.Add(111, "Raj", "Nagpur",45000,"IT");
        dt.Rows.Add(222, "Neha", "Kanpur",20000,"Accounts");
        dt.Rows.Add(333, "Amit","New York",30000, "Management");
        dt.Rows.Add(444, "Digvijay", "Kanpur", 35000, "IT");
        dt.Rows.Add(555, "Rajesh", "Delhi", 25000, "HR");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}


Execute the program, you will get the following output.

Output:

datatable output

Important methods of DataTable

  • AcceptChanges: It saves changes which done with records in a DataTable.
  • Clear: This method clears all data from DataTable.
  • Clone: It creates the copy of table with schema and constraints.
  • GetChanges: Get the table containing the changes.
  • Load: It is used to fill the table using data source.
  • Merge: It is used to merge the current table with specified table.
  • NewRow: It creates a new row with same schema of DataTable.
  • ReadXML: It reads the XML using the XMLReader.
  • WriteXML: Writes the current data table into the file.
  • RejectChange: This method is used to rollback changes that previously done.

Add new row to the table

If you want to add a new row on some event at run time then you can do it as follows.
Take a button control and write code as given below.

protected void btnAddNewRow_Click(object sender, EventArgs e)
{
        DataRow dr = dt.NewRow();
        dr["EmployeeID"] = 666;
        dr["Name"] = "Atul";
        dr["City"] = "Mumbai";
        dr["Salary"] = 12000;
        dr["Department"] = "CS";
       //Add the new row to datatable
        dt.Rows.Add(dr);        
        GridView1.DataBind();
}


Working with DataSet

DataSet is the core object of ADO.NET. DataSet and DataTable works in disconnected mode. DataSet object behaves like a small database. It can contain more than one table and also create the relationship between tables. Again DataTable is the collection of DataRow and DataColomn objects.

One of the advantages of dataset is that it can work with different types of databases simultaneously. Suppose that one table is available within SQL server and another table is available in Oracle. You can easily query each database server and create a single DataSet with both the table.  

Example

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default : System.Web.UI.Page
{
    SqlConnection conn;
    SqlDataAdapter adapter;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        string cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        try
        {
            conn = new SqlConnection(cs);
            adapter = new SqlDataAdapter("select * from tblEmps", conn);
            ds = new DataSet();
            adapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch(Exception ex)
        {
            Label1.Text = "ERROR :: " + ex.Message;
        }
        finally
        {
            ds.Dispose();
            conn.Dispose();
        }        
    }    
}


Understanding DataAdapter

DataAdapter is an important object in ADO.NET. DataAdapter works as a bridge between DataSet and your DataSource. It holds the SQL commands and connection object for reading and writing data. DataSet does not open or close the connection because this task is performed by DataAdapter object. It helps to manage the data in disconnected mode.

DataAdapter performs the following tasks when using with DataSet object:

1. Open connection
2. Fills the DataSet
3. Close connection

It can also perform Select, Insert, Update and Delete SQL operations with the database.

dataadapter operation

Difference between DataSet and DataReader

DataSetDataReader
Works in connected mode.It provides connection oriented environment.
Provides slow performance compare to DataReader.Provides the fast execution.
In memory object. You can fetch the data in any order.It is a forward-only and read only object.
Implicitly open the connection.It needs explicit open and close the connection.
It can contain more than one table.At a time, it works on single table.
Dataset objects have XML Support.It does not fully support XML
It uses fill() method of DataAdapter to populate the dataset.DataReader object provides the read() method for reading the records.