private void PerformPageSetup(TrainingDataEntities context)
        {
            // get the current culture info
            DateTimeFormatInfo formatInfo = CultureInfo.CurrentCulture.DateTimeFormat;


            // populate the month select element
            monthSelect.Items.Clear();
            for (int i = 1; i < 13; i++)
            {
                monthSelect.Items.Add(formatInfo.GetAbbreviatedMonthName(i));
            }


            // get the current data 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
            eventTypeSelect.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);
            }
        }
        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;
            }
        }
        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);
        }
Beispiel #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);
                }
            }
        }
        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();
            }
        }
Beispiel #6
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)
                {
                    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());
                    }
                }
            }
        }
        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();
            }
        }
        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;
                        }

                        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");
                }
            }
        }
 public static string[] GetEventTypeNames(TrainingDataEntities entityContext)
 {
     return(entityContext.EventTypes.Select(e => e.Name).ToArray());
 }
 public static IEnumerable <Event> GetEventsByType(TrainingDataEntities entityContext, string typeParam)
 {
     return(entityContext.Events.Where(e => e.Type == typeParam).Select(e => e));
 }
 public static IEnumerable <Event> GetAllEvents(TrainingDataEntities entityContext)
 {
     return(entityContext.Events);
 }
 public static RankingSet GetPersonalRanking(TrainingDataEntities entityContext, Event eventParam)
 {
     return(new RankingSet(entityContext.GetPersonalRanking1(eventParam.Athlete, eventParam.Type, eventParam.SwimTime, eventParam.CycleTime, eventParam.RunTime, eventParam.OverallTime)));
 }
 public static string[] GetAthleteNames(TrainingDataEntities entityContext)
 {
     return(entityContext.Athletes.Select(a => a.Name).ToArray());
 }