public void SaveOrUpdate(RefillProductTypeDataEntity p_type)
 {
     using(var session = NHibernateHelper.OpenSession())
     {
         using(var transaction = session.BeginTransaction())
         {
             try
             {
                 session.SaveOrUpdate(p_type);
                 transaction.Commit();
             }
             catch(Exception ex)
             {
                 transaction.Rollback();
                 throw ex;
             }
         }
     }
 }
 public void DeleteProductType(RefillProductTypeDataEntity productType)
 {
     m_productTypeDao.Delete(productType);
 }
        private List<RefillProductTypeDataEntity> GetProductTypeDataValueChange(List<int> rowIndexChange, out string errorMessage)
        {
            List<RefillProductTypeDataEntity> productTypes = new List<RefillProductTypeDataEntity>();
            RefillProductTypeDataEntity productType = null;
            RefillProductTypeDataEntity newProduct = null;
            List<string> updatedProducts = new List<string>();
            string errorMsg = string.Empty;
            string name = string.Empty;
            string description = string.Empty;
            decimal price = 0;
            bool isNameExists = true;

            foreach(int rowIndex in rowIndexChange)
            {
                productType = new RefillProductTypeDataEntity();
                newProduct = new RefillProductTypeDataEntity();
                name = this.dgvProductType.Rows[rowIndex].Cells["Name"].Value.ToString().Trim();

                if(this.dgvProductType.Rows[rowIndex].Cells["Description"].Value != null)
                    description = this.dgvProductType.Rows[rowIndex].Cells["Description"].Value.ToString().Trim();

                price = Convert.ToDecimal(this.dgvProductType.Rows[rowIndex].Cells["Price"].Value.ToString());

                newProduct = m_productTypeEntity.Find(m_productType => m_productType.Name.ToUpper() == name.ToUpper());
                if(newProduct == null || newProduct.ProductTypeID == 0)
                    isNameExists = false;

                if(rowIndex < productTypeMaxRowIndex)
                {
                    int productTypeID = (int)this.dgvProductType.Rows[rowIndex].Cells["ProductTypeID"].Value;
                    productType = m_productTypeEntity.Find(m_productType => m_productType.ProductTypeID == productTypeID);

                    if(productType.Name.ToUpper().Equals(name.ToUpper()))
                    {
                        productType.Name = name;
                        productType.Description = description;
                        productType.Price = price;
                        productTypes.Add(productType);
                    }
                    else if(!productType.Name.ToUpper().Equals(name.ToUpper())
                            && !isNameExists && !updatedProducts.Contains(name.ToUpper()))
                    {
                        productType.Name = name;
                        productType.Description = description;
                        productType.Price = price;
                        productTypes.Add(productType);
                        updatedProducts.Add(productType.Name.ToUpper());
                    }
                    else
                    {
                        errorMsg += name + " , ";
                    }
                }
                else
                {
                    if(!isNameExists && !updatedProducts.Contains(name.ToUpper()))
                    {
                        productType.Name = name;
                        productType.Description = description;
                        productType.Price = price;

                        updatedProducts.Add(productType.Name.ToUpper());
                        productTypes.Add(productType);
                    }
                    else
                    {
                        errorMsg += name + " , ";
                    }
                }

            }
            errorMessage = errorMsg;
            return productTypes;
        }
        void BtnDeleteProductClick(object sender, EventArgs e)
        {
            RefillProductTypeDataEntity productType = new RefillProductTypeDataEntity();

            if(this.dgvProductType.SelectedRows.Count > 0)
            {
                if(MessageService.ShowYesNo("Are you sure to delete the selected Product Type?", "Delete Product Type"))
                {
                    foreach(DataGridViewRow currentRow in this.dgvProductType.SelectedRows)
                    {
                        if(currentRow.Index < productTypeMaxRowIndex)
                        {
                            int productTypeID = (int)this.dgvProductType.Rows[currentRow.Index].Cells["ProductTypeID"].Value;
                            productType = m_productTypeEntity.Find(m_productType => m_productType.ProductTypeID == productTypeID);
                            m_presenter.DeleteProductType(productType);
                        }

                        if(!this.dgvProductType.Rows[currentRow.Index].IsNewRow)
                            dgvProductType.Rows.Remove(this.dgvProductType.Rows[currentRow.Index]);
                    }
                    m_presenter.SetAllRefillProductType();
                    productTypeMaxRowIndex = this.dgvProductType.RowCount - 1;
                    productTypeIndexChange.Clear();
                }
            }
        }