Using INotifyPropertyChanged Event Handler

naimish's Avatar author of Using INotifyPropertyChanged Event Handler
This is an article on Using INotifyPropertyChanged Event Handler in ASP.NET.
Rated 5.00 By 1 users

Introduction



Changing values in databse when a property changes is cumbersome to make it easier we can use propertchanged Event as in the solution.

Background



A script is generated for every property changed when the user wishes to update (Save button clicked) we can run this script.

We do not require to create query for all properties the same function can be used accross all properties thus making it much easier for us to update.

The code




Code: .NET

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.ComponentModel;
namespace Addresses
{
    class Phone : BaseConnection, IAdd, INotifyPropertyChanged
    {
        string phoneNo;
        string carrier;
        //public string CmdStr;
        string tableName = "PhoneNo";
        bool IsNew;
        public string PhoneNo
        {
            get
            {
                return phoneNo;
            }
            set
            {
                phoneNo = value;
                NotifyPropertyChanged(PhoneNo.GetType(), "PhoneNo", value);
                //CmdStr += "UPDATE PhoneNo SET PhoneNo=" + value + ";";
            }
        }
        public string Carrier
        {
            get
            {
                return carrier;
            }
            set
            {
                carrier = value;
                NotifyPropertyChanged(Carrier.GetType(),"Carrier", value);
                //CmdStr += "UPDATE PhoneNo SET Carrier=" + value + ";";
            }
        }
        public Phone(string PhoneId)
            : base()
        {
            cmd.CommandText = "SELECT PhoneNo, Carrier FROM PhoneNo WHERE PhoneNo='" + PhoneId+"';";
            if (conn.State!=ConnectionState.Open)
                conn.Open();
                
            OleDbDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            phoneNo = rdr[0].ToString();
            carrier = rdr[1].ToString();
            rdr.Close();
            if (ConnectionOpener == this.GetType())
                conn.Close();
            IsNew = false;
            
        }
        public Phone(string phNo, string carr)
            : base()
        {
            phoneNo = phNo;
            carrier = carr;
            IsNew = true;
            AddToBuffer();
        }
        #region IAdd Members
        public void Add(IAdd add)
        {
            Phone phone = (Phone)add;
            cmd.CommandText = "INSERT INTO PhoneNo (PhoneNo, Carrier) VALUES( @phoneNo,@Carrier)";
            OleDbParameter p1 = new OleDbParameter("@phoneNo", phone.phoneNo);
            OleDbParameter p2 = new OleDbParameter("@Carrier", phone.Carrier);
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            if (conn.State != ConnectionState.Open)
                conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch
            {
            //    throw new Exception();
            }
            if (ConnectionOpener == this.GetType())
                conn.Close();
        }

        public void AddToBuffer()
        {
            CmdStr = " INSERT INTO PhoneNo (PhoneNo, Carrier) VALUES( "+this.phoneNo+","+this.carrier+");";
        }
        #endregion
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(Type type,string propertyName,String propertyValue)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyValue));
            }
            switch(type.Name)
            {
                case "String":
            CmdStr += " UPDATE " + this.tableName + " SET "+propertyName+"=" + propertyValue + ";";
                    break;
                case "Int32":
                    CmdStr += " UPDATE " + this.tableName + " SET "+propertyName+"=" + propertyValue + ";";
                break;
                default:
                    break;
            }
        }
 
        #endregion
    }
}
Go4Expert Founder
3Oct2009,17:06   #2
shabbir's Avatar
Nomination for Article of the month - Sep 2009 Started. Nominate this article.