Typed Dataset and its Usages
page 3 of 10
by Satheesh Babu
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 45184/ 39

Creating typed dataset

1) Open a web application project in Visual Studio 2005.

2) To add a typed dataset, right click the solution and click “Add New Item.” It will open a new item dialog box like below.

Figure 1 – Add New Item

3) Select Dataset and change the name; click Add.

4) Open the server explorer in visual studio. If you cannott find the server explorer, click “Server Explorer" from the view menu of visual studio.

5) It will open Add Connection window. Select the Server name and select the “NorthWind” database or your own database as below.

Figure 2 – Add Connection

Select Server name, select the database name and click OK.

6) Drag and drop the Employees table into the dataset designer to create the typed dataset from the server explorer. The designer will look like figure 3.

Figure 3 – Dataset Designer

 

By default, it will have a TableAdapter (EmployeesTableAdapter here) with Fill() and GetData() methods that can be used to fill and fetch data from the database without implementing a line of code.

Intellisense Support in Visual Studio 2005

Visual Studio 2005 helps us with the intellisense support (Refer to Figure 4) by listing tables, columns and methods for the constructed typed dataset.

Figure 4 – Visual Studio Intellisense

“_Employees[0]” in the intellisense is the table name and “_” is because we used the same name for the dataset name as the table name. If the Dataset name is different then the table, the name will be “Employees.”

How to use it?

We can use the typed dataset and fill it in the normal way as we do for a non-typed dataset.

Listing 2: Filling Typed Dataset in a usual way

com = new SqlCommand("Select * from Employees", sqlcon); 
ada = new SqlDataAdapter(com);          
emp = new Employees();
da.Fill(emp,"Employees");

The table name in the fill method should match the table name in the typed dataset.

Using EmployeesTableAdapter:

Listing 3: Filling Typed Dataset using TableAdapter

BO.EmployeesTableAdapters.EmployeesTableAdapter ada = new 
    BO.EmployeesTableAdapters.EmployeesTableAdapter();
gvEmployees.DataSource = ada.GetData();
gvEmployees.DataBind();

In the above code “BO” is the namespace name since I have used a separate Class library project with name BO and created the typed dataset there. If you are creating typed dataset in the same web project then the above code will be:

Listing 4: Filling Typed Dataset using TableAdapter in Same project

EmployeesTableAdapters.EmployeesTableAdapter ada = new 
    EmployeesTableAdapters.EmployeesTableAdapter();
gvEmployees.DataSource = ada.GetData();
gvEmployees.DataBind();

Here, “gvEmployees” is the gridview. In the next sections we will see how to use TableAdapter and extend the typed dataset to provide custom methods that serve specific purposes. We use the “customer” table in NorthWind database for this article.

Figure 5 – Customer Table

 

What is a TableAdapter?

TableAdapters provide communication between your application and a database. More specifically, a TableAdapter connects to a database, executes queries or stored procedures, and either returns a new data table populated with the returned data or fills an existing DataTable with the returned data. TableAdapters are also used to send updated data from your application back to the database.

Adding New Functionality

We will try to add a new Selection functionality and a new update functionality using TableAdapter in Typed dataset using visual studio.


View Entire Article

User Comments

Title: Why good for only very small projects?   
Name: Mark
Date: 2010-11-14 5:14:00 PM
Comment:
Why are Typed DataSets viable only if our project sizes are really small? How about a hybrid: BOs in the middle and UI tiers; Typed DataSets at the DataLayer side--Resolving data-changes from BOs to DataSets?
Title: I need urgent help plz   
Name: Fadi
Date: 2010-06-13 2:10:47 PM
Comment:
hi there, i am using typed dataset for my rdlc reports in my winforms app with C#. i m inserting data in database form different forms. the problem is, on run time when i insert data in the database its not updated data in rdlc reports, to get new data in my reports i need to close my app and debug it again to see new data in the reports. Is there any way to forcedly refresh data in my typed dataset on runtime???? Looking forward from your positive response.

Fadi
Title: developer   
Name: John Jones
Date: 2009-08-27 5:19:58 PM
Comment:
One point I forgot to mention:
Our main goal is to use the same SQL queries (basic inserts, updates, deletes) in both MS Access and SQL Server. Our app was originally written for Access using the VS designer to generate the queries. After testing these queries, we found that if the ` char was removed and replaced by [ ] brackets, then all the queries would work in both db's.
Title: developer   
Name: john
Date: 2009-08-26 12:46:38 PM
Comment:
We have a DataSet with Table Adapters built for MSAccess using OLEDB. We are using Visual Studio 2005 and .Net 2.0.

When the designer creates insert, update and delete queries it encloses the table name and field names with a ` (ASCII char 96).
For example:

INSERT INTO `table` (`col1`, `col2`, `col3`) values (....)

Is there a reason for this character? Assuming it's for escape purposes, is there a setting to make Visual Studio use another character, eg "[" or "]"?
Title: sir   
Name: jim
Date: 2008-12-15 8:18:50 PM
Comment:
where is the method SetUnitPriceNull found - not in this sample

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 5:48:09 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search