Home > Microsoft .Net Development Tips > .NET Architecture > Passing datasets via Web services
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET ARCHITECTURE

Passing datasets via Web services


Bob Tabor
10.11.2002
Rating: -4.80- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Download the video screen cam of this tip.

Datasets are a completely disconnected way of passing data between components, functions, or applications. What you may not realize is that the "killer app" for Datasets is Web services.

Datasets live a double life. When used within a .NET application, they can be accessed through ADO.NET object model in a manner similar to accessing recordsets using ADO in the past. But they also can be persisted into XML, complete with relations, and self-describing meta-data without any effort on your part. ADO.NET manages how the dataset is persisted between its in-memory representation and its XML persisted form based on a given situation. When passing a Dataset back from a Web service, ADO.NET serializes the Dataset into XML and sends it along its merry way to the Web service consumer.

To illustrate how easy Visual Studio.NET makes this entire process, I've built a small affiliate management Web service that allows an affiliate's Web site to check on how its affiliate program is going, and the commissions that it is owed by the company running the affiliate program. There are three database tables I created in SQL Server:

  1. An Affiliate table, which has information about all the affiliates in the program
  2. A Customer table, which keeps track of the customers who have made purchases, including a foreign key relationship with the Affiliate who referred this customer to the site, and finally...
  3. A Purchase table, which has the details about each purchase a particular customer has made on the site.

The Web site owner can integrate the data from this Web service into his existing customer management application, his accounting application, or into his Web site in whatever way he pleases. Again, the purpose of this is to illustrate how easy it is to create this Web service using Visual Studio.NET. I'll walk through these steps at a fairly high level, so I would encourage you to download the screen cam video showing how I created this project so you can witness the entire process from start to finish. The following are the basic steps:

  1. Start by creating the tables in SQL Server (or the MSDE that ships with Visual Studio.NET). Add some sample data to the tables.
  2. Create a new Visual Basic.NET Web Service project and rename the "service1.asmx" file to "AffiliateManagement.asmx".
  3. In the Server Explorer, create a Data Connection to the database in which you created the tables, then navigate to the Purchase table, and drag-and-drop the table onto the Web service Designer Surface. This creates a SQL Connection and a SQL Data Adapter on the Web service's Designer Surface.
  4. Select the SQL Data Adapter and select the "Configure Data Adapter..."link at the bottom of the Properties window. This opens the "Data Adapter Configuration Wizard."
  5. Use the Query Builder to create a SELECT statement that grabs all of the purchase information from the Purchase table and some customer information, (ESPECIALLY the affiliate_id) from the Customer table. The SELECT statement looks like this:
    SELECT purchase.purchase_id, purchase.purchase_date, purchase.purchase_amount, 
    purchase.customer_id, purchase.commission_due, purchase.commission_paid_date, 
    customer.customer_name, customer.customer_email, customer.customer_id 
    FROM purchase 
    INNER JOIN customer ON purchase.customer_id = customer.customer_id WHERE 
    (customer.affiliate_id = @affiliate)
    

    Notice that I've included a parameter (@affiliate), which allows me to pass in a specific affiliate_id for the affiliate I will search on. I allow the wizard to create the stored procedure for me.

  6. Next, select the SQL Data Adapter again, and this time, select the "Generate Dataset..." link under the Properties window. From here you can create a Data Set called dsAffiliatePurchase and add an instance of it to the Web service Designer Surface.
  7. Now, access the code behind for the Web service and create a Web Method that looks like this:
    <WebMethod()> _
    Public Function GetAffiliateDetail(ByVal AffiliateID As Integer) _
       As dsAffiliatePurchase
    
       SqlDataAdapter1.SelectCommand.Parameters.Item("@affiliate").Value = 
    AffiliateID
       SqlDataAdapter1.Fill(DsAffiliatePurchase1, "purchase")
       GetAffiliateDetail = DsAffiliatePurchase1
    
    End Function
    

Notice that the GetAffiliateDetail Web Method returns an instance of dsAffiliatePurchase. How will it do this? We let ADO.NET worry about the details of serializing the Dataset into XML. The first line of code sets the @affiliate parameter to the value passed into the Web service by the consumer. The second line of code "fills" the instance of the dataset that was added to our Designer Surface into the "purchase" table (which is just a name... in this case it does not have the actual structure of the "purchase" table but was named that since we dragged-and-dropped originally from the Server Explorer in step 4 above). The third line of code then sets the results in the dataset to the return value for the Web Method.

When you compile and test using the Web service Help Page to enter the AffiliateID, you'll see the values of the dataset passed back as serialized into XML.

Again, due to space constraints, I went over the steps at a high level, and would encourage you to download the video screen cam that demonstrates all of these steps more easily than words can express. In a few basic steps, you can expose information from your database to your trading partners, affiliates or potential customers through Web services, ADO.NET and Visual Studio.NET.

But the REAL magic happens in the next several tips, which shows how the XML is then persisted back into an ADO.NET dataset, and how changes to Datasets by the client of the Web service are recorded into Datagrams and sent back to the Web service provider for processing. Stay tuned.



About the Author

Robert Tabor is a Microsoft Certified Professional in Visual Basic with over six years of experience developing n-tier Microsoft-centric applications for some of the world's most prestigious companies and consulting organizations, such as Ernst & Young, KPMG, Cambridge Technology Partners, Sprint, American Heart Association, and the Mary Kay Corporation. Bob is the author of Microsoft .NET XML Web services by Sams Publishing, and contributes to SoapWebservices.com and LearnVisualStudio.NET. He is currently working on initiatives within Mary Kay, the second largest eCommerce site in retail volume on the net, of how to utilize .NET within their e-business group.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
.NET Architecture
Microsoft's Oslo modeling platform, the M language and .NET
Introduction to ASP.NET's Model View Controller (MVC) Design Pattern
Free book from Microsoft brings patterns and practices to .NET
A look at concurrency constructs and primitives in .NET
Designing Windows Communication Foundation service contracts
Book excerpt: Java EE and .NET security interoperability
Book excerpt: .NET Framework Design Guidelines
Book Excerpt: Executing activities in Windows Workflow Foundation
Aspect-oriented programming (AOP) and the .NET Framework
Book excerpt: Hands-on Windows Communication Foundation

Web services and SOA implementations in the .NET Framework
Goin' mobile with Windows
Microsoft "WebSphere Loves Windows" ads tout cost, performance vs. AIX
Microsoft releases new CTP of Oslo SDK
Microsoft's Oslo modeling platform, the M language and .NET
Outgoing Bill Gates says UML on tap in Oslo SOA modeler
Podcast: Windows CardSpace authors speak
Open XML SDK ready for the road
Some of the Zen of Volta
Scaling WCF applications can challenge development teams
Security interoperability with .NET/WSE and WebLogic Workshop 8.1

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
scripting language  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts