Click here to Skip to main content
15,896,730 members
Articles / Web Development / HTML

AJAX DataGrid

Rate me:
Please Sign up or sign in to vote.
4.57/5 (23 votes)
5 Jun 20074 min read 110.4K   2K   105  
This article demonstrates the use of the AJAX technique in tandem with the JavaScript DataGrid from my previous article.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using AndrewGolik.DaoTraining.TransferObjects;
using AndrewGolik.DaoTraining.Common.Utilities;

namespace AndrewGolik.DaoTraining.DataAccessLayer.XmlRepository 
{

    public class XmlCustomerDao:CustomerDao
    {
        private Object thisLock = new Object();
                   
        public  int CreateCustomer(Customer customer)
        {


            lock (thisLock)
            {
                
                XmlDocument xmlCustomers = new XmlDocument();
                xmlCustomers.Load(XmlDaoFactory.PathToCustomersFile);
                //generate the new id for the customer
                XmlNode xmlLastGeneratedId = xmlCustomers.DocumentElement.Attributes.GetNamedItem("LastGeneratedId");
                int lastGeneratedIdValue = Convert.ToInt32(xmlLastGeneratedId.Value) + 1;
                xmlLastGeneratedId.InnerText = lastGeneratedIdValue.ToString();
                //assign the new generated id to the customer then serialize it to xml
                customer.CustomerID = lastGeneratedIdValue;
                XmlDocument xmlCustomer = Serialization.FillXmlDocFromObject<Customer>(customer);
                //import new customer xml to customers list
                xmlCustomers.DocumentElement.AppendChild(xmlCustomers.ImportNode(xmlCustomer.DocumentElement,true));
                xmlCustomers.Save(XmlDaoFactory.PathToCustomersFile);
                return lastGeneratedIdValue;

            }
            
          
           
                 
        }

        public  bool DeleteCustomer(int customerID) 
        {
            lock(thisLock)
            {   //remove all related orders
                //note: this calling is optional. If the datasource supports "cascade removing related records"
                //you can just ignore this calling. For instance, it's ignored in MsSqlDao
                 
                DeleteOrders(customerID);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XmlDaoFactory.PathToCustomersFile);
                XmlNode xmlCutomerNode = xmlDoc.DocumentElement.SelectSingleNode("Customer[CustomerID = " + customerID + "]");
                if (xmlCutomerNode != null)
                {
                    xmlCutomerNode.ParentNode.RemoveChild(xmlCutomerNode);
                    xmlDoc.Save(XmlDaoFactory.PathToCustomersFile);
                    return true;
                }
                else
                    return false;
            }
        }
        
        public bool DeleteOrders(int customerID) 
        {

            lock (thisLock)
            {

                XmlDocument xmlOrders = new XmlDocument();
                xmlOrders.Load(XmlDaoFactory.PathToOrdersFile);
                XmlNodeList orders = xmlOrders.DocumentElement.SelectNodes("Order[CustomerID="+customerID+"]");
                if (orders.Count == 0)
                    return false;
                for (int index=0;index < orders.Count;index++)
                {
                    orders[index].ParentNode.RemoveChild(orders[index]);
                }
                xmlOrders.Save(XmlDaoFactory.PathToOrdersFile);
                return true;
        
            }
            
        }
       
      
        public  bool UpdateCustomer(Customer customer) 
        {



            lock (thisLock)
            {   //first remove customer to update
                if (DeleteCustomer(customer.CustomerID))
                {
                    XmlDocument xmlCustomer = Serialization.FillXmlDocFromObject<Customer>(customer);
                    //import customer xml updated to customers list
                    XmlDocument xmlCustomers = new XmlDocument();
                    xmlCustomers.Load(XmlDaoFactory.PathToCustomersFile);
                    xmlCustomers.DocumentElement.AppendChild(xmlCustomers.ImportNode(xmlCustomer.DocumentElement, true));
                    xmlCustomers.Save(XmlDaoFactory.PathToCustomersFile);
                    return true;
                }
                else 
                 
                    return false;
                 
            }


          

        }

        public List<Customer> GetCustomers() 
        { 

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(XmlDaoFactory.PathToCustomersFile);
            XmlNodeList xmlCustomerList = xmlDoc.DocumentElement.SelectNodes("Customer");
            List<Customer> customers = new List<Customer>();
            for(int index=0; index < xmlCustomerList.Count; index++)
            {
                XmlDocument xmlCustomer = new XmlDocument();
                xmlCustomer.LoadXml(xmlCustomerList[index].OuterXml);
                Customer customer =  Serialization.FillObjectFromXMLDoc<Customer>(xmlCustomer);
                customers.Add(customer);

            }
            return customers;

        }
       
    }
}

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
Belarus Belarus
Andrew Golik is a software professional working in Minsk, Belarus.
He enjoys design infrastructures based on object oriented paradigm. His programming experience includes ASP, ASP.NET, .NET, COM, JAVA, PHP, DHTML, AJAX, blah blah blah....

Andrew Golik's Blog

Comments and Discussions