protected void btnOK_Click1(object sender, EventArgs e)
    {
        //create a new instance of clsStock
        clsStock someStock = new clsStock();
        //capture the inputs as strings

        String ItemName      = txtItemName.Text;
        String ItemType      = txtItemType.Text;
        String StockQuantity = txtStockQuantity.Text;
        String Price         = txtPrice.Text;
        String Supplier      = txtSupplier.Text;
        String NextRestock   = txtNextRestock.Text;
        String Error         = "";

        // validate the data
        Error = someStock.Valid(ItemName, ItemType, StockQuantity, Price, Supplier, NextRestock);
        if (Error == "")//If it was fine collect the data
        {
            someStock.ItemID        = ItemID;
            someStock.ItemName      = ItemName;
            someStock.ItemType      = ItemType;
            someStock.StockQuantity = Int32.Parse(StockQuantity);
            someStock.Price         = Double.Parse(Price);
            someStock.Supplier      = Supplier;
            someStock.NextRestock   = Convert.ToDateTime(NextRestock);
            //create a new instance of item collection
            clsItemCollection ItemList = new clsItemCollection();
            //if this is a new record i.e. ItemID = -1 then add the data
            if (someStock.ItemID == -1)
            {
                someStock.ItemID  = Int32.Parse(txtItemID.Text);
                ItemList.ThisItem = someStock;

                ItemList.Add();
            }
            //otherwise it must be an update
            else
            {
                ItemList.ThisItem.Find(someStock.ItemID);
                ItemList.ThisItem = someStock;
                ItemList.Update();
            }
            Response.Redirect("StockList.aspx");
        }
        else
        {
            lblError.Text = Error;
        }
    }
示例#2
0
        public void UpdateMethodOK()
        {
            clsItemCollection AllItems   = new clsItemCollection();
            clsStock          TestItem   = new clsStock();
            Int32             PrimaryKey = 0;

            //set properties
            TestItem.ItemID        = 11; //This will only work once because it adds the test item to the list meaning there is now a duplicate, so change this value each time or remove it from the database
            TestItem.Available     = true;
            TestItem.ItemName      = "TestItem";
            TestItem.ItemType      = "Monitor";
            TestItem.StockQuantity = 10;
            TestItem.Price         = 10.00;
            TestItem.Supplier      = "KappaInc";
            TestItem.NextRestock   = DateTime.Now.Date.AddDays(1);
            //set ThisItem to the test data
            AllItems.ThisItem = TestItem;
            //add the record
            PrimaryKey = AllItems.Add();
            //set the primary key of the test data
            TestItem.ItemID = PrimaryKey;
            //modify the test data
            TestItem.Available     = false;
            TestItem.ItemName      = "ADifferentTestItem";
            TestItem.ItemType      = "Mouse";
            TestItem.StockQuantity = 33;
            TestItem.Price         = 25.99;
            TestItem.Supplier      = "NotKappaInc";
            TestItem.NextRestock   = DateTime.Now.Date.AddDays(1);
            //set the record based on the new data
            AllItems.ThisItem = TestItem;
            //update the record
            AllItems.Update();
            //find the record
            AllItems.ThisItem.Find(PrimaryKey);
            //test to see that the two values are the same
            Assert.AreEqual(AllItems.ThisItem, TestItem);
        }