Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Learn to Use the New Data Source Controls in ASP.NET 2.0 : Page 2

You can declaratively connect your data sources to Web form controls with ease in ASP.NET 2.0. In this article, three new data source controls are described in detail: ObjectDataSource, DataSetDataSource, and XmlDataSource.


advertisement
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

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap