Databinding is not working properly for nested object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saumitra Kumar Paul
    New Member
    • Aug 2010
    • 12

    Databinding is not working properly for nested object

    I save a "SalesRetur n" object in my library which has few objects as property.

    Code:
        namespace BusinessObjects
        {
        public class SalesReturn : ObjectBase
        {
    
            private Int32 pSalesReturnId;
            private SalesPerson pSalesPersonDetails=new SalesPerson();
            private PackSize pPackSizeDetails=new PackSize();
         
            public Int32 SalesReturnId
            {
                get { return pSalesReturnId; }
                set
                {
                    if (!value.Equals(pSalesReturnId))
                    {
                        pSalesReturnId = value;
                        PropertyHasChanged("SalesReturnId");
                    }
                }
            }
    
            public SalesPerson SalesPersonDetails
            {
                get { return pSalesPersonDetails; }
                set
                {
                    if (!value.Equals(pSalesPersonDetails))
                    {
                        pSalesPersonDetails = value;
                        PropertyHasChanged("SalesPersonDetails");
                    }
                }
            }
    
            public PackSize PackSizeDetails
            {
                get { return pPackSizeDetails; }
                set
                {
                    if (!value.Equals(pPackSizeDetails))
                    {
                        pPackSizeDetails = value;
                        PropertyHasChanged("PackSizeDetails");
                    }
                }
            }
    
            public SalesReturn()
            {
            }
    
               
            public static SalesReturn FillEntity(SQLiteDataReader Reader,SQLiteConnection Connection)
            {
                SalesReturn salesreturn = new SalesReturn();
                salesreturn.pSalesReturnId = Convert.ToInt32(Reader["SalesReturnId"]);
                salesreturn.SalesPersonDetails =SalesPerson.GetEntity( Convert.ToInt32(Reader["SalesPersonId"]),Connection);
                salesreturn.pPackSizeDetails =PackSize.GetEntity( Convert.ToInt32(Reader["PackSizeId"]),Connection);
                salesreturn.MarkOld();
                return salesreturn;
            }
    
            public static SalesReturn GetEntity(int salesReturnId, string ConnectionString)
            {
                SalesReturn salesreturn = null;
                using (SQLiteConnection Connection = new SQLiteConnection(ConnectionString))
                {
                    Connection.Open();
                    string sqlSelect = "SELECT SalesReturnId, SalesPersonId, PackSizeId FROM tblSalesReturn WHERE SalesReturnId=" + salesReturnId + "";
                    using (SQLiteCommand cmd = new SQLiteCommand(sqlSelect, Connection))
                    {
                        SQLiteDataReader Reader = cmd.ExecuteReader();
                        if (Reader.HasRows)
                        {
                            Reader.Read();
                            salesreturn = FillEntity(Reader, Connection);
                        }
                        if (!Reader.IsClosed)
                        {
                            Reader.Close();
                        }
                    }
                    Connection.Close();
                }
                return salesreturn;
            }
    
        }
        }
    I have a WinForm databound to this object. I am using a method to bind to that object.
    Code:
            private void AddDataBindings()
            {
                bsSalesPerson.DataSource = SalesPerson.GetComboList(GlobalVariables.ConnectionString);
                cmbSalesPersonDetails.DataSource = bsSalesPerson;
                cmbSalesPersonDetails.ValueMember = "SalesPersonId";
                cmbSalesPersonDetails.DisplayMember = "FullNameInEnglish";
    
                bsPackSize.DataSource = PackSize.GetComboList(GlobalVariables.ConnectionString);
                cmbPackSizeDetails.DataSource = bsPackSize;
                cmbPackSizeDetails.ValueMember = "PackSizeId";
                cmbPackSizeDetails.DisplayMember = "ProductPackSize";
                lblUnitName.DataBindings.Add("Text", bsPackSize, "UnitName");
    
                bsCurrentEntity.DataSource = typeof(SalesReturn);
                cmbSalesPersonDetails.DataBindings.Add("SelectedItem", bsCurrentEntity, "SalesPersonDetails",false,DataSourceUpdateMode.OnPropertyChanged);
                cmbPackSizeDetails.DataBindings.Add("SelectedItem", bsCurrentEntity, "PackSizeDetails", false, DataSourceUpdateMode.OnPropertyChanged);
            }
    My problem is when i retrieve an existing "SalesRetur n" object from database, databound comboboxes are not showing the appropriate value.
    Code:
            private void LoadObject(object sender, EventArgs e)
            {
                    CurrentEntity = SalesReturn.GetEntity(salesReturnId, GlobalVariables.ConnectionString);
                    bsCurrentEntity.DataSource = CurrentEntity;       
            }
    I could not identify the problem. Would you please help me to identify it?
Working...