private void PerformPageSetup(TrainingDataEntities context)
        {
            DateTimeFormatInfo formatInfo = CultureInfo.CurrentCulture.DateTimeFormat;

            monthSelect.Items.Clear();
            for (int i = 1; i < 13; i++)
            {
                monthSelect.Items.Add(formatInfo.GetAbbreviatedMonthName(i));
            }

            // get the current date and use it to select a month and set the day and year
            DateTime now = DateTime.Now;

            monthSelect.SelectedIndex = now.Month - 1;
            dayInput.Value            = now.Day.ToString();
            yearInput.Value           = now.Year.ToString();

            // populate the athlete names
            athleteSelect.Items.Clear();
            foreach (string name in DataAccess.GetAthleteNames(context))
            {
                athleteSelect.Items.Add(name);
            }

            // populate the event types
            eventTypeSelect.Items.Clear();
            foreach (string name in DataAccess.GetEventTypeNames(context))
            {
                eventTypeSelect.Items.Add(name);
            }
        }
Exemple #2
0
        public static void AddEvent(TrainingDataEntities entityContext, DateTime timeParam, string athleteParam, string typeParam, TimeSpan swimTimeParam, TimeSpan cycleTimeParam, TimeSpan runTimeParam)
        {
            Event newEvent = new Event()
            {
                Date        = timeParam,
                Athlete     = athleteParam,
                Type        = typeParam,
                SwimTime    = swimTimeParam,
                CycleTime   = cycleTimeParam,
                RunTime     = runTimeParam,
                OverallTime = swimTimeParam + cycleTimeParam + runTimeParam
            };

            entityContext.Events.Add(newEvent);
            try
            {
                entityContext.SaveChanges();
            }
            catch (InvalidOperationException ex)
            {
                //ex.Message;
                throw ex;
            }
            catch (Exception ex)
            {
                // ex.InnerException.Message;
                throw ex;
            }
        }
Exemple #3
0
        public static Event GetEventByID(TrainingDataEntities entityContext, int keyParam)
        {
            // query for the ID
            IEnumerable <Event> results = entityContext.Events.Where(e => e.ID == keyParam).Select(e => e);

            // as the ID is primary key, there will be zero or on results
            return(results.Count() == 1 ? results.First() : null);
        }
Exemple #4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // create the entity data model context object
            using (TrainingDataEntities context = new TrainingDataEntities())
            {
                // populate the select control if needed
                if (ViewState["setupComplete"] == null)
                {
                    foreach (string name in context.EventTypes.Select(item => item.Name))
                    {
                        eventSelector.Items.Add(name);
                    }
                    ViewState["setupComplete"] = true;
                }
                // define the collection of events that we will process
                IEnumerable <Event> eventsToProcess;

                if (IsPostBack && eventSelector.Value != "All")
                {
                    // perform a LINQ query to filter the data

                    /*
                     * eventsToProcess = from item in context.Events
                     *                where item.Type == eventSelector.Value
                     *                select item;
                     */
                    eventsToProcess = DataAccess.GetEventsByType(context, eventSelector.Value);
                }
                else
                {
                    /* eventsToProcess = context.Events; */
                    eventsToProcess = DataAccess.GetAllEvents(context);
                }


                // enumerate the objects in the context.Events property - these correspond to the rows in the Events table in the database
                foreach (Event ev in eventsToProcess)
                {
                    /*
                     * int personalRank = new RankingSet(
                     *    context.GetPersonalRanking1(ev.Athlete, ev.Type, ev.SwimTime, ev.CycleTime, ev.RunTime, ev.OverallTime)
                     * ).OverallRank;
                     * int referenceRank = new RankingSet(
                     *    context.GetReferenceRanking1(ev.Type, ev.SwimTime, ev.CycleTime, ev.RunTime, ev.OverallTime)
                     *  ).OverallRank;
                     */
                    int personalRank = DataAccess.GetPersonalRanking(context, ev).OverallRank;

                    int referenceRank = DataAccess.GetReferenceRanking(context, ev).OverallRank;

                    // process the enitity object
                    ProcessEvent(ev, personalRank, referenceRank);
                }
            }
        }
Exemple #5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            using (TrainingDataEntities context = new TrainingDataEntities())
            {
                DataTotals totals = DataAccess.GetDataTotals(context);

                eventCountSpan.InnerText = totals.EventTotal.ToString();
                mileCountSpan.InnerText  = string.Format("{0:F1}", totals.MileTotal);
                hourCountSpan.InnerText  = string.Format("{0} Hours and {1} Minutes", totals.TimeTotal.Hours, totals.TimeTotal.Minutes);
            }
        }
Exemple #6
0
        public static void DeleteEventByID(TrainingDataEntities entityContext, int keyParam)
        {
            // query for the object that has the specified key
            Event targetEvent = GetEventByID(entityContext, keyParam);

            if (targetEvent != null)
            {
                entityContext.Events.Remove(targetEvent);
                entityContext.SaveChanges();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            using (TrainingDataEntities context = new TrainingDataEntities())
            {
                if (ViewState["setupComplete"] == null)
                {
                    PerformPageSetup(context);
                    ViewState["setupComplete"] = true;
                }

                if (IsPostBack)
                {
                    try
                    {
                        int day   = int.Parse(dayInput.Value);
                        int month = monthSelect.SelectedIndex + 1;
                        int year  = int.Parse(yearInput.Value);
                        DataAccess.AddEvent(
                            context,
                            new DateTime(year, month, day),
                            athleteSelect.Value,
                            eventTypeSelect.Value,
                            TimeSpan.Parse(swimTimeInput.Value),
                            TimeSpan.Parse(cycleTimeInput.Value),
                            TimeSpan.Parse(runTimeInput.Value)
                            );

                        Response.Redirect("EventList.aspx");
                    }
                    catch (FormatException)
                    {
                        errorDiv.InnerText = "Cannot parse inputs";
                    }
                    catch (InvalidOperationException ex)
                    {
                        errorDiv.InnerText = "Invalid Operation Exception: " + ex.Message;
                    }
                    catch (ArgumentNullException ex)
                    {
                        errorDiv.InnerText = "ArgumentNull Exception " + ex.Message;
                    }
                    catch (OverflowException ex)
                    {
                        errorDiv.InnerHtml = string.Format("<p>{0}</p> <p>{1}</p>", ex.Message, ex.Source);
                    }
                    catch (Exception ex)
                    {
                        errorDiv.InnerText = string.Format("<p>{0}</p> <p>{1}</p> <p>{2}</p>", ex.InnerException.Message, ex.InnerException.StackTrace, ex.InnerException.ToString());
                    }
                }
            }
        }
Exemple #8
0
        public static DataTotals GetDataTotals(TrainingDataEntities entityContext)
        {
            DataTotals totals = new DataTotals();

            totals.EventTotal = entityContext.Events.Count();

            // we need to iterate to work out the distance and time totals
            foreach (Event ev in entityContext.Events)
            {
                // work out the total time by summing the OverallTime values
                totals.TimeTotal += ev.OverallTime;
                // work out the total miles by using the navigation properties
                EventType type = ev.EventType;
                totals.MileTotal += (type.SwimMiles + type.CycleMiles + type.RunMiles);
            }

            return(totals);
        }
Exemple #9
0
        public static void UpdateEvent(TrainingDataEntities entityContext, int keyParam, DateTime dateParam, string athleteParam, string typeParam, TimeSpan swimTimeParam, TimeSpan cycleTimeParam, TimeSpan runTimeParam)
        {
            // query for the event with the specified key
            Event targetEvent = GetEventByID(entityContext, keyParam);

            // set the param valuesfor the event
            if (targetEvent != null)
            {
                // update the event object properties
                targetEvent.Date        = dateParam;
                targetEvent.Athlete     = athleteParam;
                targetEvent.Type        = typeParam;
                targetEvent.SwimTime    = swimTimeParam;
                targetEvent.CycleTime   = cycleTimeParam;
                targetEvent.RunTime     = runTimeParam;
                targetEvent.OverallTime = swimTimeParam + cycleTimeParam + runTimeParam;

                entityContext.SaveChanges();
            }
        }
Exemple #10
0
        protected void Page_Load(object sender, EventArgs e)
        {
            using (TrainingDataEntities context = new TrainingDataEntities())
            {
                if (ViewState["setupComplete"] == null)
                {
                    PerformPageSetup(context);
                    ViewState["setupComplete"] = true;
                }

                if (!IsPostBack)
                {
                    string mode;
                    int    eventID;
                    Event  targetEvent;
                    if (
                        (mode = Request.QueryString["mode"]) != null &&
                        int.TryParse(Request.QueryString["id"], out eventID) &&
                        (targetEvent = DataAccess.GetEventByID(context, eventID)) != null
                        )
                    {
                        // set the hidden fields in the form
                        this.modeInput.Value = mode;
                        this.keyInput.Value  = eventID.ToString();

                        // use the property values of the event to populate page controls
                        monthSelect.SelectedIndex = targetEvent.Date.Month - 1;
                        dayInput.Value            = targetEvent.Date.Day.ToString();
                        yearInput.Value           = targetEvent.Date.Year.ToString();

                        // set the selected index for the athlete and event controls
                        SetSelectedIndex(athleteSelect, targetEvent.Athlete);
                        SetSelectedIndex(eventTypeSelect, targetEvent.Type);

                        // set the times
                        swimTimeInput.Value  = targetEvent.SwimTime.ToString();
                        cycleTimeInput.Value = targetEvent.CycleTime.ToString();
                        runTimeInput.Value   = targetEvent.RunTime.ToString();

                        if (mode == "delete")
                        {
                            monthSelect.Disabled     = true;
                            dayInput.Disabled        = true;
                            yearInput.Disabled       = true;
                            athleteSelect.Disabled   = true;
                            eventTypeSelect.Disabled = true;
                            swimTimeInput.Disabled   = true;
                            cycleTimeInput.Disabled  = true;
                            runTimeInput.Disabled    = true;

                            foreach (string controlID in new string[] { "titleDiv", "footerDiv" })
                            {
                                HtmlControl ctrl = Master.FindControl(controlID) as HtmlControl;
                                if (ctrl != null)
                                {
                                    ctrl.Style["background"] = "#980000";
                                }
                            }
                        }

                        button.Value = mode == "edit" ? "Save" : "Delete";
                    }
                    else
                    {
                        Response.Redirect("/ListEvent.aspx");
                    }
                }
                else
                {
                    if (modeInput.Value == "edit")
                    {
                        DataAccess.UpdateEvent(
                            context, int.Parse(keyInput.Value),
                            new DateTime(int.Parse(yearInput.Value), monthSelect.SelectedIndex + 1, int.Parse(dayInput.Value)),
                            athleteSelect.Value,
                            eventTypeSelect.Value,
                            TimeSpan.Parse(swimTimeInput.Value),
                            TimeSpan.Parse(cycleTimeInput.Value),
                            TimeSpan.Parse(runTimeInput.Value)
                            );
                    }
                    else
                    {
                        DataAccess.DeleteEventByID(context, int.Parse(keyInput.Value));
                    }

                    Response.Redirect("/EventList.aspx");
                }
            }
        }
Exemple #11
0
 public static IEnumerable <Event> GetEventsByType(TrainingDataEntities entityContext, string typeParam)
 {
     return(entityContext.Events.Where(e => e.Type == typeParam).Select(e => e));
 }
Exemple #12
0
 public static IEnumerable <Event> GetAllEvents(TrainingDataEntities entityContext)
 {
     return(entityContext.Events);
 }
Exemple #13
0
 public static RankingSet GetPersonalRanking(TrainingDataEntities entityContext, Event eventParam)
 {
     return(new RankingSet(entityContext.GetPersonalRanking(eventParam.Athlete, eventParam.Type, eventParam.SwimTime, eventParam.CycleTime, eventParam.RunTime, eventParam.OverallTime)));
 }
Exemple #14
0
 public static string[] GetAthleteNames(TrainingDataEntities entityContext)
 {
     return(entityContext.Athletes.Select(a => a.Name).ToArray());
 }
Exemple #15
0
 public static string[] GetEventTypeNames(TrainingDataEntities entityContext)
 {
     return(entityContext.EventTypes.Select(e => e.Name).ToArray());
 }