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

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

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
27 May 2010CPOL2 min read 74.2K   1.3K   68  
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());
                ddlFindProperty.DataBind();

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

            }
        }
        /// <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
                objLst.Add(piObj.Name);
            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;
            grdGrid.DataBind();
        }
        
        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.

License

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


Written By
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.

Comments and Discussions