Microsoft Knowledge Base Email Alertz

KBAlertz.com: (322793) - The step-by-step article demonstrates how to use the DataAdapter TableMappings collection to map the generic DataTable object names to the schema that is already defined in the typed DataSet object. When you use a DataAdapter object to fill a DataSet...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 2.0 Web Hosting with SQL 2005: Click Here!
Discount ASP.NET Hosting


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




How To Fill a Typed DataSet from a Stored Procedure That Returns Multiple Resultsets by Using Visual Basic .NET

Article ID:322793
Last Review:August 30, 2004
Revision:2.2
This article was previously published under Q322793
On This Page

SUMMARY

The step-by-step article demonstrates how to use the DataAdapter TableMappings collection to map the generic DataTable object names to the schema that is already defined in the typed DataSet object.

When you use a DataAdapter object to fill a DataSet from a stored procedure that returns multiple resultsets, the DataAdapter creates DataTable objects named Table, Table1, Table2, and so on in the DataSet. To use more meaningful names, you can rename the DataTable objects. However, you cannot use this solution with a typed DataSet.

Back to the top

Create the Stored Procedure

The stored procedure in this example uses data from the Microsoft SQL Server Northwind sample database. The stored procedure selects all of the records from the Customers table and then selects all of the records from the Orders table.
1.Start Microsoft Visual Studio .NET.
2.On the View menu, click Server Explorer.
3.In Server Explorer, connect to your SQL Server Northwind database.
4.In the tree view, right-click the Stored Procedure node under your database connection, and then click New Stored Procedure.
5.Add the following code to the stored procedure:
CREATE PROCEDURE dbo.sp_GetCustOrd
AS
   SELECT * FROM Customers;
   SELECT * FROM Orders;
					
6.On the File menu, click Save to save the stored procedure.

Back to the top

Create the Typed DataSet

Although you can create the typed DataSet schema manually by adding a new DataSet object to the project (on the Project menu, click Add New Item, and then click DataSet), this example uses Server Explorer to build the DataSet schema automatically.
1.In Visual Studio .NET, on the File menu, point to New, and then click Project.
2.Click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
3.On the View menu, click Server Explorer.
4.In Server Explorer, click Connect to Database, and then connect to your SQL Server Northwind database.
5.Drag the Customers and the Orders tables from Server Explorer to the current project. Notice that a SqlConnection object and two SqlDataAdapter objects are added to the project.
6.In the Properties window, click Generate Dataset, and then add both tables to the DataSet. Notice that an .xsd file is added to the project. The .xsd file is named according to the name that you chose for the DataSet class.

Back to the top

Add the DataRelation

1.In Solution Explorer, double-click the .xsd file that you created in the previous section.
2.Right-click the Customers table in the designer, point to Add, and then click New Relation.
3.In the Edit Relation dialog box, select the Orders table as the child element, and then click OK. This creates a new DataRelation named CustomersOrders in the DataSet schema.
4.On the File menu, click Save to save the changes.

Back to the top

Fill the Typed DataSet

This section adds code to fill the typed DataSet and to map the table names. This section also uses the DataGrid control to display the filled DataSet.
1.Drag a DataGrid control from the toolbox to the form.
2.Double-click the form, and then add the following code to the Load event of the form:
Dim da As New SqlDataAdapter("sp_GetCustOrd", SqlConnection1)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.TableMappings.Add("Table", "Customers")
da.TableMappings.Add("Table1", "Orders")
Dim ds As New CustOrd()       ' Change this name to match .xsd file name.
da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Customers"
						
NOTE: You must change "CustOrd" to match the name of the .xsd file that you created in the Create the Typed DataSet section.
3.Add the following namespace reference at the top of the code window:
Imports System.Data.SqlClient
					
4.On the File menu, click Save to save the changes.
5.Press F5 to run the application.

Back to the top

Troubleshooting

•You cannot have multistatement stored procedures when you use Microsoft Jet databases.
•Stored procedure syntax may vary with relational database systems other than Microsoft SQL Server.
•The SqlClient .NET Data Provider only works with Microsoft SQL Server 7.0 and later. For any other relational database system, use the OleDb .NET Data Provider or other suitable provider, and then include the appropriate connection information.
•Typically, the System and the System.Data namespaces are imported at a project level for Visual Basic projects. If you are compiling from the command prompt, or if you have changed your project settings, you may have to explicitly add Imports statements for these namespaces at the top of the code window.

Back to the top


APPLIES TO
•Microsoft ADO.NET (included with the .NET Framework)
•Microsoft ADO.NET 1.1
•Microsoft Visual Basic .NET 2002 Standard Edition
•Microsoft Visual Basic .NET 2003 Standard Edition

Back to the top

Keywords: 
kbhowto kbsqlclient kbsystemdata kbdataadapter KB322793

Back to the top

       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Dheeraj Chauhan Report As Irrelevant  
Written: 5/16/2006 10:14 PM
Very good artical............

Celia - c.hosano NOSPAM-AT-NOSPAM gmail.com Report As Irrelevant  
Written: 7/31/2006 3:29 PM
Hi Scott, I tried to Create the Typed DataSet, but I could not find where to click to connect to the database. I also have a question, how do we fill a typed dataset from multiple stored procedures? Can we do that? Also, from .xsd file, can we know which store procedure is used? or what are the data behind it? Thank you so much in advance.

Anonymous User Reported as Irrelevant  
Written: 10/18/2006 2:49 AM
from code below .. da.TableMappings.Add("Table", "Customers") da.TableMappings.Add("Table1", "Orders") how do we know that "Table" must be the returned data from "Customers" table or "Table1" must be the returned data from "Orders" table ? Is it depend on the sequence of the result return from dbo.sp_GetCustOrd ?

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please