advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 3/5 | Rate this item | 2 users have rated this item.
Email this articleEmail this article
Learn to Use the New Data Source Controls in ASP.NET 2.0 (cont'd)
The ObjectDataSource Control
The ObjectDataSource control allows binding your business objects to data controls such as the DataGrid control. To see how the ObjectDataSource control works, create a new Web site project in Visual Studio 2005 Beta 1 and add a new class called Products to the project:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class Products
    Public Function getProducts() As DataSet
        Dim conn As New SqlConnection("Server=(local);Integrated Security=True;Database=Northwind;Persist 
Security Info=True") Dim adapter As New SqlDataAdapter("SELECT [ProductID], [ProductName], [SupplierID], [CategoryID],
[QuantityPerUnit], [UnitPrice] FROM [Products]", conn) Dim ds As New DataSet adapter.Fill(ds, "Products") Return ds End Function End Class
advertisement
The Products class contains a getProducts method that returns a dataset containing all the products in the Northwind database. Using the ObjectDataSource control, you can allow a data control to bind to this class. To do so, drag-and-drop an ObjectDataSource control onto the default page (see Figure 2).

Figure 4. Get Products: Choose a method to bind to the Select operation.
Click on the 'Configure Data Source…' link in the Smart Tag. You will see the window shown in Figure 3. Select the name of the business object—in this case it is the Products class—and click Next.

In the next window, you will select the method to use for each operation, such as Select, Update, Insert, and Delete. For this example, the Products class has only the getProducts method, so choose the getProducts method for the Select tab (see Figure 4) and click Finish.

Figure 6. Getting Wind of Northwind: The default .aspx page displays the records using the GridView control.
Next, drag-and-drop a GridView control to the page and bind it to ObjectDataSource1. Check the three options available in the Smart Tag—Enable Paging, Enable Scripting, and Enable Selection (see Figure 5).

That's it! Press F5 to load the page. You will be able to see the records in the Products table in the Northwind database in the GridView control (see Figure 6).

What about editing rows in the table through the ObjectDataSource control? To do that, you need to have another method in the Products class to perform the editing work. So, add a new method called updateProducts to the Products class:


Public Sub updateProducts(ByVal ProductID As Integer, _
                      ByVal ProductName As String, _
                      ByVal SupplierID As Integer, _
                      ByVal CategoryID As Integer, _
                      ByVal QuantityPerUnit As String, _
                      ByVal UnitPrice As Double)
   Dim conn As New SqlConnection("Server=(local);Integrated 
Security=True;Database=Northwind;Persist Security 
Info=True")
   Dim adapter As New SqlDataAdapter("SELECT * FROM Products WHERE ProductID=" & ProductID, conn)
   Dim ds As New DataSet
   adapter.Fill(ds, "Products")
   With ds.Tables(0).Rows(0)
       .Item("ProductName") = ProductName
       .Item("SupplierID") = SupplierID
       .Item("CategoryID") = CategoryID
       .Item("QuantityPerUnit") = QuantityPerUnit
       .Item("UnitPrice") = UnitPrice
   End With
   Dim cb As New SqlCommandBuilder(adapter)
   adapter.Update(ds, "Products")
End Sub
Now configure the ObjectDataSource control again. This time round, click on the Update tab, and select the updateProducts method. Then click Finish (see Figure 7).

In the Smart Tag of the GridView control, check the Enable Editing option (see Figure 8). Then, press F5 to load the page again. You can now edit the rows in the table (see Figure 9). For deletion, you can likewise create a new method to perform a deletion and then update the ObjectDataSource control again.


Figure 8. Add a Checkbox: Enable editing on the GridView control.
 
Figure 9. Aniseed Syrup? Edit a row in the GridView control.

Previous Page: Introduction Next Page: The DataSetDataSource Control
Page 1: IntroductionPage 3: The DataSetDataSource Control
Page 2: The ObjectDataSource ControlPage 4: The XmlDataSource Control
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES