Using INotifyPropertyChanged Event Handler

Discussion in 'ASP.NET' started by naimish, Sep 17, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    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
        }
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice