A typed DataSet is a class that derives from a DataSet. As such, it inherits all the methods, events, and properties of a DataSet. Typed DataSet uses information in the Schema file. Additionally, a typed DataSet provides strongly typed methods, events, and properties. This means you can access tables and columns by name, instead of using collection-based methods.
Now here I will present a example to create a Typed Dataset and pass this from UI layer to DataAccess layer and back to populate values on a Datagrid.
To create a Typed Data Set, left click on the solution explorer and select add -> Add New Item. Now from the dialogue box select Dataset, and name it CustTypedDs.xsd. Now open the Server explorer and drag and drop the “Customers” Table, from the Northwind database.
In the UI layer (Customer.aspx), place a button & DataGrid. On button click event, create an object, which is reference to Data Access Layer and call the function “FnCustomers”. This function returns a typed dataset which data is displayed on the datagrid.
“FnCustomers” is a function in the Data Access Layer which is going to take data from database and fill the Typed dataset and will pass it to UI Layer.
CODE -
Customer.aspx.cs
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using TDS;
namespace TDS { public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DataGrid DataGrid1;
DAccessLayer ObjDAccessLayer=new DAccessLayer(); CustTypedDs ObjSchema=new CustTypedDs(); private void Page_Load(object sender, System.EventArgs e) { }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void Button1_Click(object sender, System.EventArgs e) { ObjDAccessLayer.FnCustomers(ObjSchema); DataGrid1.DataSource=ObjDAccessLayer.FnCustomers(ObjSchema);; DataGrid1.DataBind(); } } }
DataccessLayer.cs
using System; using System.Data; using System.Data.SqlClient; using TDS;
namespace TDS { public class DAccessLayer { public CustTypedDs FnCustomers(CustTypedDs dsObj) { //Get the connection from Web.Config file String ObjVarConnection= System.Configuration.ConfigurationSettings.AppSettings["Connection"] ; SqlConnection ObjConnection= new SqlConnection(ObjVarConnection); SqlDataAdapter da = new SqlDataAdapter("select * from Customers",ObjConnection); da.Fill(dsObj,"Customers"); } } }
Hope after going through this article you would got to know the benifit of using a Typed Dataset and how easy it is to use it.
|
| Author: Sudheer Reddy 30 Apr 2005 | Member Level: Silver Points : 0 |
Thank you for your article... I have few Q's on this article.. can you clarify please. 1. What are differences b/w Typed & UnTyped Datasets. 2. Performence wise which is better one?
Sudheer.
|