protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            CatalogManager manager = new CatalogManager();
            RequisitionItem item = new RequisitionItem();
            item.StationeryID = Convert.ToInt32(((DropDownList)DetailsView1.FindControl("stDDL")).SelectedValue);
            item.QuantityRequested = Convert.ToInt32(((TextBox)DetailsView1.FindControl("stTextBox")).Text);
            item.QuantityIssued = 0;
            item.Price = 0;

            if (requisition.RequisitionItems.Count == 0)
            {
                requisition.RequisitionItems.Add(item);
            }
            else
            {
                foreach (var req in requisition.RequisitionItems)
                {
                    if (item.StationeryID == req.StationeryID)
                    {
                        req.QuantityRequested += item.QuantityRequested;

                        break;
                    }
                    else
                    {
                        requisition.RequisitionItems.Add(item);
                        break;
                    }
                }

            }
            GridDataBind();
        }
        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            RequisitionItem item = new RequisitionItem();

            item.StationeryID = Convert.ToInt32(((DropDownList)DetailsView1.FindControl("StationeryDDL")).SelectedValue);
            item.QuantityRequested = Convert.ToInt32(((TextBox)DetailsView1.FindControl("QuantityTextBox")).Text);
            item.QuantityIssued = 0;
            item.Price = 0;

            Debug.WriteLine(item.StationeryID);
            Debug.WriteLine(item.QuantityRequested);
        }
        /// <summary>
        /// Update the requisitionItem
        /// </summary>
        /// <param name="requisitionItem">requisitionItem object</param>
        public void UpdateRequisitionItem(RequisitionItem requisitionItem)
        {
            try
            {
                var temp = (from ri in context.RequisitionItems
                            where ri.RequisitionItemID == requisitionItem.RequisitionItemID
                            select ri).FirstOrDefault<RequisitionItem>();
                temp.StationeryID = requisitionItem.StationeryID;
                temp.QuantityRequested = requisitionItem.QuantityRequested;
                temp.Price = requisitionItem.Price;

                context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Delete the requisitionItem
        /// </summary>
        /// <param name="requisitionItem">requisitionItem object</param>
        public void DeleteRequisitionItem(RequisitionItem requisitionItem)
        {
            try
            {
                var temp = (from ri in context.RequisitionItems
                            where ri.RequisitionID == requisitionItem.RequisitionID
                            select ri).FirstOrDefault<RequisitionItem>();

                context.RequisitionItems.DeleteObject(temp);

                context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Create a new requisitionItem
        /// </summary>
        /// <param name="requisitionItem">requisitionItem object</param>
        public void CreateRequisitionItem(RequisitionItem requisitionItem)
        {
            try
            {
                context.AddToRequisitionItems(requisitionItem);
            }
            catch (Exception)
            {

                throw;
            }
        }
 /// <summary>
 /// Update the requisitionItem
 /// </summary>
 /// <param name="requisitionItem">requisitionItem object</param>
 public void UpdateRequisitionItem(RequisitionItem requisitionItem)
 {
     try
     {
         RequisitionItem temp = requisitionDAO.GetRequisitionItemsByID(requisitionItem.RequisitionItemID);
         if (ValidateRequisitionItem(temp, RequisitionMethod.Update))
         {
             temp.Stationery = requisitionItem.Stationery;
             temp.QuantityRequested = requisitionItem.QuantityRequested;
             //temp.Price = requisitionItem.Price;
             requisitionDAO.UpdateRequisitionItem(temp);
         }
         else
         {
             ErrorMessage("Update item failed");
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
        /// <summary>
        /// Delete the requisitionItem
        /// </summary>
        /// <param name="requisitionItem">requisitionItem object</param>
        public void DeleteRequisitionItem(RequisitionItem requisitionItem)
        {
            try
            {
                RequisitionItem temp = requisitionDAO.GetRequisitionItemsByID(requisitionItem.RequisitionItemID);
                if (temp != null)
                {
                    requisitionDAO.DeleteRequisitionItem(temp);
                }
                else
                {
                    ErrorMessage("Delete item failed");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Create a new requisitionItem
        /// </summary>
        /// <param name="requisitionItem">requisitionItem object</param>
        public void CreateRequisitionItem(RequisitionItem requisitionItem)
        {
            try
            {
                if (requisitionItem != null && ValidateRequisitionItem(requisitionItem, RequisitionMethod.Create))
                {
                    requisitionDAO.CreateRequisitionItem(requisitionItem);
                }
                else
                {
                    ErrorMessage("Add item failed");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Validate RequisitionItem
        /// </summary>
        /// <param name="requisitionItem">requisitionItem object</param>
        /// <param name="requisitionMethod">enum requisitionMethod</param>
        /// <returns>boolean</returns>
        private bool ValidateRequisitionItem(RequisitionItem requisitionItem, RequisitionMethod requisitionMethod)
        {
            try
            {
                if (requisitionItem != null)
                {
                    if (requisitionMethod == RequisitionMethod.Create)
                    {
                        if ((requisitionItem.RequisitionID != 0 || requisitionItem.Requisition != null) &&
                           (requisitionItem.StationeryID != 0 || requisitionItem.Stationery != null) &&
                           (requisitionItem.QuantityRequested > 0))
                        {
                            return true;
                        }
                    }

                    if (requisitionMethod == RequisitionMethod.Update)
                    {
                        if ((requisitionItem.RequisitionID != 0 || requisitionItem.Requisition != null) &&
                         (requisitionItem.StationeryID != 0 || requisitionItem.Stationery != null) &&
                         (requisitionItem.QuantityRequested > 0))
                        {
                            return true;
                        }
                    }

                    if (requisitionMethod == RequisitionMethod.Delete)
                    {
                        if (requisitionItem.RequisitionItemID != 0)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //Create instance of new requisition
            Requisition r = new Requisition();

            UserDAO user = new UserDAO();
            RequisitionManager rq = new RequisitionManager();
            CatalogDAO cat = new CatalogDAO();

            //set the properties of requisition object
            r.DepartmentID = user.GetDepartmentByID(1).DepartmentID;
            r.CreatedBy = user.GetUserByID(1).UserID;
            r.StatusID = rq.GetStatusByID(new Status() { StatusID = 1}).StatusID;
            r.ApprovedBy = user.GetUserByID(2).UserID;
            r.UrgencyID = rq.GetUrgencyByID(new Urgency() { UrgencyID = 1 }).UrgencyID;
            r.RequisitionForm = "test";
            r.DateRequested = DateTime.Now;
            //r.DateApproved = DateTime.Now;

            //Create a new requisitionitem for the current requisition
            RequisitionItem rqi = new RequisitionItem()
            {
                RequisitionID = r.RequisitionID,
                StationeryID = cat.GetAllStationeries().FirstOrDefault<Stationery>().StationeryID,
                QuantityRequested = 10,
                QuantityIssued = 10,
                Price = 5

            };

            //Create a new specialrequisitionitem for the current requisition
            SpecialRequisitionItem spi = new SpecialRequisitionItem()
            {
                RequisitionID = r.RequisitionID,
                SpecialStationeryID = 1,
                QuantityRequested = 10,
                QuantityIssued = 10,
                Price = 5,
                Name = "arav",
                Description = "test"

            };

            //Add Child objects of the requisition objects
            //Add requisitionitem to requisition object
            r.RequisitionItems.Add(rqi);

            //Add specialitem to requisition object
            r.SpecialRequisitionItems.Add(spi);

            //Persist requisition to database
            //EF is very intelligent. It will also persist to requistionitem and specialrequistionitem
            rq.CreateRequisition(r);

            r = rq.GetAllRequisition().Last<Requisition>();
            r.RequisitionItems.Last<RequisitionItem>().QuantityIssued = 6;

            rq.UpdateRequisition(r);

            spi = rq.GetAllSpecialRequisitionItems(r).Last<SpecialRequisitionItem>();

            rq.DeleteSpecialRequisitionItem(spi);

            //Testing databinding after creation of requisitions
            if (!IsPostBack)
            {
                GridView1.DataSource = rq.GetAllRequisitionItems(r);
                GridView1.DataBind();

                if (r != null)
                {
                    GridView2.DataSource = rq.GetAllRequisition();
                    GridView2.DataBind();
                }
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the RequisitionItems EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToRequisitionItems(RequisitionItem requisitionItem)
 {
     base.AddObject("RequisitionItems", requisitionItem);
 }
 /// <summary>
 /// Create a new RequisitionItem object.
 /// </summary>
 /// <param name="requisitionItemID">Initial value of the RequisitionItemID property.</param>
 /// <param name="requisitionID">Initial value of the RequisitionID property.</param>
 /// <param name="stationeryID">Initial value of the StationeryID property.</param>
 /// <param name="quantityRequested">Initial value of the QuantityRequested property.</param>
 /// <param name="quantityIssued">Initial value of the QuantityIssued property.</param>
 /// <param name="price">Initial value of the Price property.</param>
 public static RequisitionItem CreateRequisitionItem(global::System.Int32 requisitionItemID, global::System.Int32 requisitionID, global::System.Int32 stationeryID, global::System.Int32 quantityRequested, global::System.Int32 quantityIssued, global::System.Decimal price)
 {
     RequisitionItem requisitionItem = new RequisitionItem();
     requisitionItem.RequisitionItemID = requisitionItemID;
     requisitionItem.RequisitionID = requisitionID;
     requisitionItem.StationeryID = stationeryID;
     requisitionItem.QuantityRequested = quantityRequested;
     requisitionItem.QuantityIssued = quantityIssued;
     requisitionItem.Price = price;
     return requisitionItem;
 }
        private void TestMethod()
        {
            requisition.CreatedBy = 1;
            requisition.DepartmentID = 1;
            requisition.RequisitionForm = "dds/111/11";
            requisition.StatusID = 1;
            requisition.UrgencyID = 1;
            requisition.DateRequested = DateTime.Now;

            SpecialRequisitionItem splItem = new SpecialRequisitionItem()
            {
                QuantityIssued = 0,
                QuantityRequested = 10,
                Name = "test",
                Description = "desc",
                RemarkByRequester = "remarks",
                Price = 0
            };

            RequisitionItem item = new RequisitionItem()
            {
                StationeryID = 1,
                QuantityRequested = 12,
                QuantityIssued = 0,
                Price = 0
            };

            requisition.RequisitionItems.Add(item);
            requisition.SpecialRequisitionItems.Add(splItem);

            try
            {
                rDAO.CreateRequisition(requisition);
            }
            catch (Exception)
            {

                throw;
            }
        }