Beispiel #1
0
 protected void RegionSearch_Click(object sender, EventArgs e)
 {
     //verify you have input
     if (string.IsNullOrEmpty(RegionArg.Text))
     {
         MessageLabel.Text = "enter a region id";
     }
     else
     {
         //I am assuming that a number has been enter, this should
         //      really be validated
         //standard lookup
         //connect to controller CLASS by creating an instance
         RegionController sysmgr = new RegionController();
         //issue your call to the class instance
         NorthwindSystem.Entities.Region info = sysmgr.Region_FindByID(int.Parse(RegionArg.Text));
         //test results and either display record or appreciate message
         if (info == null)
         {
             MessageLabel.Text = "No region for supplied value.";
         }
         else
         {
             RegionID.Text          = info.RegionID.ToString();
             RegionDescription.Text = info.RegionDescription;
         }
     }
 }
        public int Add(Region region)
        {
            // TODO: Add Unit Test
            if (region == null)
                throw new ArgumentNullException("region", "region is null.");
            using (var dbContext = new NWContext())
            {
                /* NOTE:
                 *  The TerritoryID column in Territories is a string - nvarchar(20) - rather than an integer.
                 *  The existing data in Northwind Traders uses the zip code of the city/town as the TerritoryID.
                 *  This sample just "simplifies" and assigns the territory description as the ID, since we're
                 *  in Canada and we aren't using a single zip or postal code.
                 */
                foreach (var territory in region.Territories)
                    if (string.IsNullOrEmpty(territory.TerritoryID))
                        territory.TerritoryID = territory.TerritoryDescription;

                /* NOTE:
                 *  The RegionID column in Regions is an integer, but it is not an IDENTITY column.
                 *  As such, we're simply going to get the next highest ID available.
                 */
                if (region.RegionID <= 0)
                    region.RegionID = dbContext.Regions.Max(item => item.RegionID) + 1;

                dbContext.Regions.Add(region);

                dbContext.SaveChanges();

                return region.RegionID;
            }
        }
Beispiel #3
0
        protected void RegionSearch_Click(object sender, EventArgs e)
        {
            //verify input exists
            if (string.IsNullOrEmpty(RegionArg.Text))
            {
                MessageLabel.Text = "Enter a region id";
            }
            else
            {
                //you could do other validations such as: numeric check,
                //   range check (>0), ...

                //standard look up
                try
                {
                    //connect to controller class by creating an instance
                    RegionController sysmgr = new RegionController();

                    //issue your call to the class instance
                    NorthwindSystem.Entities.Region info = sysmgr.Region_FindByID(int.Parse(RegionArg.Text));

                    //test results and either display record or an approriate message
                    if (info == null)
                    {
                        //no found
                        MessageLabel.Text      = "No region for supplied value";
                        RegionId.Text          = "";
                        RegionDescription.Text = "";
                    }
                    else
                    {
                        //RegionId is a point to the label itself
                        //.Text is a property of the Label instance which
                        //     allow one to change its contents.
                        RegionId.Text          = info.RegionID.ToString();
                        RegionDescription.Text = info.RegionDescription;
                    }
                }
                catch (Exception ex)
                {
                    MessageLabel.Text = $"Error: {ex.Message}";
                }
            }
        }
        public void Update(Region region, List<Territory> territories)
        {
            // TODO: Add Unit Test
            if (region == null)
                throw new ArgumentNullException("region", "region is null.");
            if (territories == null)
                throw new ArgumentNullException("territories", "territories is null.");

            using (var dbContext = new NWContext())
            {
                foreach (var item in territories)
                {
                    var found = dbContext.Territories.Find(item.TerritoryID);
                    if (found != null)
                    {
                        /* NOTE:
                         *  Pre-process the Territory IDs to see if they should be "synced" with the name/description.
                         *  This will be the case if, in the original, the ID was the same as the description
                         */
                        string foundTerritoryID = found.TerritoryID;
                        string foundTerritoryDescription = found.TerritoryDescription.Trim(); // HACK: Turns out, the column is nchar(50), not an nvarchar....
                        string itemTerritoryID = item.TerritoryID;
                        string itemTerritoryDescription = item.TerritoryDescription.Trim();
                        if (foundTerritoryID.Equals(foundTerritoryDescription) &&
                            !itemTerritoryID.Equals(itemTerritoryDescription))
                        {
                            item.TerritoryID = itemTerritoryDescription;
                            dbContext.Territories.Remove(found); // Because the PK has changed...
                            dbContext.Territories.Add(item); // Because the PK has changed...
                        }
                    }
                }

                dbContext.Entry(region).State = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }