Browse DevX
Sign up for e-mail newsletters from DevX


Serializing, Consuming, and Binding Generics in .NET 2.0 : Page 3

Generics are a powerful new feature in .NET 2.0, but it's not so obvious how to use them as return values from Web services, or how to bind them to controls. Find out how to serialize, data bind, and consume generic collections in ASP.NET 2.0




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Implementing Data-binding with Generic Collections
In this section, you will see how to return generic collections from a middle tier object and bind them directly to the ObjectDataSource control. The ObjectDataSource control is then bound to a GridView control so that you can display the values returned through the generic collection in the GridView.

First, create a class named Category that acts as the placeholder for capturing attributes related to a Category. The code for the Category class is as follows:

using System; public class Category { private string _categoryID; private string _name; public string CategoryID { get{return _categoryID;} set{_categoryID = value;} } public string Name { get{return _name; } set{_name = value; } } }

As you can see, the implementation of the Category class is very simple and straightforward. It just has two properties, named CategoryID and Name. Next, here's a data access layer class named CategoryDB that leverages the Category class.

using System; using System.Data; using System.Data.SqlClient; using System.Web.Configuration; using System.Collections.Generic; public class CategoryDB { public CategoryDB() {} public IList<Category> GetCategories() { IList<Category> categoryColl = new List<Category>(); string connString = WebConfigurationManager. ConnectionStrings ["adventureWorks"]. ConnectionString; using (SqlConnection sqlConn = new SqlConnection(connString)) { sqlConn.Open(); SqlCommand sqlCmd = sqlConn.CreateCommand(); sqlCmd.CommandText = "Select " "ProductCategoryID, Name from " +"Production.ProductCategory"; //Create the SqlDataReader object using (SqlDataReader reader = sqlCmd.ExecuteReader()) { while (reader.Read()) { Category cate = new Category(); cate.CategoryID = reader["ProductCategoryID"]. ToString(); cate.Name = reader["Name"].ToString(); categoryColl.Add(cate); } } } return categoryColl; } }

The CategoryDB class code begins by declaring an object of type IList generic collection and assigning that to the generic collection of type List. Then it opens the connection to the database, executes the query and retrieves the results in a SqlDataReader object. It then loops through the returned rows retrieving values and assigning them to a new Category object, one for each row, adding each Category object to the previously declared generic collection. After adding all the rows to the collection, it returns the collection object to the caller.

For the code to work, you need to define the connection string in the web.config as shown below:

<connectionStrings> <add name="adventureWorks" connectionString= "server=localhost; database=AdventureWorks; uid=username;pwd=password;"/> </connectionStrings>

At this point, the CategoryDB object is ready for consumption; the next step is to create a page that leverages the CategoryDB.GetCategories() method. The following code defines an ASP.NET page that data-binds the output of the GetCategories() method to a GridView control.

<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Implementing Data Binding with Generics </title> </head> <body> <form id="form1" runat="server"> <div> <asp:ObjectDataSource runat="server" id="categoriesSource" typename="CategoryDB" selectmethod="GetCategories">

Figure 1. ASP.NET Page Implementing Object Binding: The ObjectDataSource Control lets you perform data binding with the "categories" data returned from the CategoryDB class.
</asp:ObjectDataSource> <asp:GridView id="gridCategories" runat="Server" DataSourceID="categoriesSource"/> <br/><br/> </div> </form> </body> </html>
Note that the page contains an ObjectDataSource control named categoriesSource with its TypeName and SelectMethod attributes set to CategoryDB and GetCategories respectively. The page binds a GridView named gridCategories to the ObjectDataSource through its DataSourceID attribute. Loading the page in a browser results in the output shown in Figure 1.

Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date