Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET

Populating custom business objects by binding business objects to a GridView using the ObjectDataSource in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.30/5 (18 votes)
20 Jan 20063 min read 113.4K   1.3K   51  
A complete article on how to populate custom business objects using a common method, binding these objects to a GridView using the ObjectDataSource control.
/*
 * This is the base class for all Businsess Objects
 * It Contains Common method for populating business object
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;


   public abstract class AWBase
   {
       protected string SqlSelectString;
       protected Type BusObjectType;          
       protected List<AWBase> LoadData()
       {
           List<AWBase> _list = new List<AWBase>();
           DBHelper dbHelper = new DBHelper();
           //Get Data 
           DataTable dtBusinessObject = dbHelper.RetrieveTable(SqlSelectString);
           //Custom Attributes
           object[] attributes = BusObjectType.GetCustomAttributes(typeof(MapInfo), false);
           //Loop through all records in Table
           if (dtBusinessObject != null && dtBusinessObject.Rows.Count > 1)
           {
               for (int introw = 0; introw < dtBusinessObject.Rows.Count; introw++)
               {
                   AWBase busObject = (AWBase)Activator.CreateInstance(BusObjectType);
                   //loop through all custom attributes
                   for (int i = 0; i < attributes.Length; i++)
                   {
                       MapInfo mapinfo = (MapInfo)attributes[i];
                       object DBValue = dtBusinessObject.Rows[introw][mapinfo.DBFieldName];
                       PropertyInfo pinfo = BusObjectType.GetProperty(mapinfo.ObjPropertyName);                       
                       pinfo.SetValue(busObject, DBValue.GetType() == typeof(DBNull) ? null: DBValue, null);
                   }
                   _list.Add(busObject);
               }
           }
           return _list;
       }       
   }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
MCSD.NET Early Achiever,MCDBA SQL 2000,MCITP SQL 2005 Charter Member,MCPD Charter Member

Comments and Discussions