Click here to Skip to main content
12,956,512 members (74,967 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads


68 bookmarked
Posted 27 May 2010

CRUD (Create, Read, Update, Delete) Operation With Generic List

, 27 May 2010 CPOL
CRUD (Create, Read, Update, Delete) Operation With Generic List
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SolutionArch.BusinessObject;
using SolutionArch.Collection.Entity;
using System.Collections;
using System.Xml.Linq;
using System.Reflection;

namespace EntityCollection
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!Page.IsPostBack)
                bindGrid(grdUser, PopulateUsers());

                ddlFindProperty.DataSource = PopulateFindProperty(new UserBO());

                ddlSortProperty.DataSource = PopulateFindProperty(new UserBO());

        /// <summary>
        /// Fetch list of property associate with input parameter
        /// </summary>
        /// <param name="obj">Business Object - UserBO etc</param>
        /// <returns>Collection with list of property</returns>
        private List<string> PopulateFindProperty(Object obj)
            List<string>    objLst  = new List<string>();
            Type            typObj  = obj.GetType(); //Reflection - Class details, like name,property,method
            PropertyInfo[]  piObjs  = typObj.GetProperties(); // List of object properties 
            foreach (PropertyInfo piObj in piObjs)// Loop through each property
            return objLst;

        /// <summary>
        /// Fetch list of record from datasource, Here we are using XML file as a datasource
        /// I real life it sjould be replaced with database driven record
        /// </summary>
        /// <returns>Collection with list of record</returns>
        private EntityCollection<UserBO> PopulateUsers()
            string      strFilePath     =   Server.MapPath("App_Data\\DummyData.xml");
            XDocument   xmlDoc          =   XDocument.Load(strFilePath);
            //Linq to XML
            var         objCustomers       = from customer in xmlDoc.Descendants("Customer")
                                                select new
                                                    Name    = customer.Element("Name").Value,
                                                    Email   = customer.Element("Email").Value,

            UserBO objUser = null;

            EntityCollection<UserBO> objUserCollection = new EntityCollection<UserBO>();

            foreach (var objCustomer in objCustomers)
                objUser         = new UserBO();
                objUser.Name    = objCustomer.Name;
                objUser.Email   = objCustomer.Email;
                objUserCollection.Add(objUser); //Populating the collection
            return objUserCollection;

        /// <summary>
        /// Bind grid with datasource
        /// </summary>
        /// <typeparam name="T">Generic Datasource</typeparam>
        /// <param name="grdGrid">Grid View</param>
        /// <param name="objCollection">datasource</param>
        private void bindGrid<T>(GridView grdGrid , EntityCollection<T> objCollection)
            grdGrid.DataSource = objCollection;
        protected void btnFind_Click(object sender, EventArgs e)
            EntityCollection<UserBO>    userCollection      = PopulateUsers();
            string                      strFindExpression   = ddlFindProperty.Text + "=" + txtFindVal.Text.Trim();

            userCollection = userCollection.FindByExpression(strFindExpression);
            bindGrid(grdFilterUser, userCollection);

        protected void btnDelete_Click(object sender, EventArgs e)
            EntityCollection<UserBO>    userCollection      = PopulateUsers();
            string                      strFindExpression   = ddlFindProperty.Text + "=" + txtFindVal.Text.Trim();

            userCollection = userCollection.DeleteByExpression(strFindExpression);
            bindGrid(grdFilterUser, userCollection);

        protected void cmdSort_Click(object sender, EventArgs e)
            EntityCollection<UserBO>    userCollection      = PopulateUsers();
            string                      strFindExpression   = ddlSortProperty.Text;

            if (ddlSortOption.SelectedItem.Text.Equals("Dsc")) strFindExpression = strFindExpression + " desc";

            userCollection = userCollection.SortByExpression(strFindExpression); // Default sort - ASC
            bindGrid(grdFilterUser, userCollection);

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bibhas Paul
Software Developer (Senior) Reputed MNC in Kolkata
India India
Bibhas has 8 years of extensive experience in application development with exposure to business requirement study, system analysis and designing, coding ,testing,
implementation, end user training and client Interaction.

He was the part of application development team and worked for industry leading organizations like "ConocoPhillips", "Abbey National Bank" and “DHL".

Posses sound experience as a technical architect and all part of software development lifecycle.

His interest includes in Microsoft Technologies (ASP.NET 3.5 & SQL Server), Design Pattern and wide variety of internet technologies like AJAX, JQUERY etc.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 27 May 2010
Article Copyright 2010 by Bibhas Paul
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid