Example #1
0
        // GET: Education/Details/5
        public async Task <ActionResult> Details(int?id)
        {
            ////indien een nullable int? id
            if (id == null)
            {
                return(BadRequest());
            }
            var edu = await educationRepo.GetEducationAsync(id.Value);

            if (edu == null)
            {
                //Of: Zorgt voor taalafhankelijk browser error:
                //return NotFound(); //404
                //return BadRequest(); //400
                ////Of: Zorgt voor customised error volgens jouw Viewmodel:
                var errorvm = new ErrorViewModel();

                errorvm.RequestId      = Convert.ToString(id.Value);
                errorvm.HttpStatuscode = System.Net.HttpStatusCode.NotFound;
                return(View("~/Views/Shared/_Error.cshtml", errorvm));
            }
            return(View(edu));
        }
Example #2
0
        //SQL helpers --------------------------------------------------
        private async Task <List <Student> > GetData(SqlDataReader reader)
        {
            List <Student> lst = new List <Student>();

            //1. try catch verhindert applicatie crash
            try
            {
                while (await reader.ReadAsync())
                {
                    Student s = new Student();
                    s.Id   = Convert.ToInt32(reader["Id"]);
                    s.Name = !Convert.IsDBNull(reader["Name"]) ? (string)reader["Name"] : "";
                    //TO DO: verder uitbouwen van overige properties
                    s.Email    = !Convert.IsDBNull(reader["Email"]) ? (string)reader["Email"] : "";
                    s.Password = !Convert.IsDBNull(reader["Password"]) ? (string)reader["Password"] : "";
                    s.Gender   = Convert.ToInt32(reader["Gender"]) == 0 ? Person.GenderType.Male : Person.GenderType.Female;
                    if (!Convert.IsDBNull(reader["DateOfBirth"]))
                    {
                        s.Birthday = Convert.ToDateTime(reader["DateOfBirth"]);
                    }
                    //EducationId kan NULL zijn.
                    if (!Convert.IsDBNull(reader["EducationId"]))
                    {
                        s.EducationId = (int)reader["EducationId"];
                        s.Education   = await educationRepo.GetEducationAsync(s.EducationId.Value);
                    }

                    //Let op mogelijke NULL waarden (=> anders crash)
                    lst.Add(s);
                }
            }
            catch (Exception exc)
            {
                Console.Write(exc.Message); //later loggen
            }
            finally
            {
                reader.Close();  //Niet vergeten. Beperkt aantal verbindingen (of kosten)
            }
            return(lst);
        }
        //READ --------------------------
        public async Task <IEnumerable <Student> > GetAllStudentsAsync(string search
                                                                       = null)
        {
            IEnumerable <Student> result = null;

            if (string.IsNullOrEmpty(search))
            {
                result = await context.Students.ToListAsync();

                foreach (Student s in result)
                {
                    s.Education = await educationRepo.GetEducationAsync(s.EducationId);
                }
            }
            else
            {
                var query = context.Students.Where(e => e.Name.Contains(search));
                result = await query.ToListAsync();
            }
            return(result.OrderBy(e => e.Name));
        }