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();
}
}
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();
}
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();
}
}
}
DataSet | DataReader |
---|---|
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. |