Beispiel #1
0
        public async Task LoadDetails()
        {
            using (var ctx = new TestSchoolScriptEntities())
            {
                var student = await ctx.Students.SingleAsync(el => el.StudentID == _studentId);

                await Task.Delay(1000);

                var studentAddress = student.StudentAddress;

                txbAddress1.Text = studentAddress?.Address1;
                txbAddress2.Text = studentAddress?.Address2;
                txbCity.Text     = studentAddress?.City;
                txbState.Text    = studentAddress?.State;

                await Task.Delay(5000);

                await ctx.Courses.LoadAsync();

                clbCourses.DataSource    = ctx.Courses.Local.ToBindingList();
                clbCourses.DisplayMember = "CourseName";

                for (int i = 0; i < clbCourses.Items.Count; i++)
                {
                    var course = clbCourses.Items[i];
                    if (student.Courses.Contains(course))
                    {
                        clbCourses.SetItemCheckState(i, CheckState.Checked);
                    }
                }

                _areDetailsLoaded = true;
            }
        }
Beispiel #2
0
 private void btnLoad_Click(object sender, EventArgs e)
 {
     try
     {
         _ctx = new TestSchoolScriptEntities();
         _ctx.Students.Load();
         this.studentBindingSource.DataSource = _ctx.Students.Local.ToBindingList();
         onGridLoadedAction(true);
     }
     catch (Exception exception)
     {
         Console.WriteLine($"{exception.Message}\n\n{exception.StackTrace}");
         MessageBox.Show(exception.StackTrace, "Error");
     }
 }
Beispiel #3
0
        private async Task <bool> SaveResults(int id)
        {
            var result = true;

            try
            {
                using (var ctx = new TestSchoolScriptEntities())
                {
                    var student = await ctx.Students.SingleAsync(el => el.StudentID == id);

                    if (student.StudentAddress == null)
                    {
                        student.StudentAddress = new StudentAddress();
                    }
                    var studentAddress = student.StudentAddress;

                    studentAddress.Address1 = txbAddress1.Text;
                    studentAddress.Address2 = txbAddress2.Text;
                    studentAddress.City     = txbCity.Text;
                    studentAddress.State    = txbState.Text;

                    var chosenCourses = clbCourses.CheckedItems.Cast <Course>();
                    student.Courses.Clear();
                    foreach (var chosenCourse in chosenCourses)
                    {
                        var contextCourse = ctx.Courses.SingleOrDefault(c => c.CourseId == chosenCourse.CourseId);
                        if (contextCourse == null)
                        {
                            continue;
                        }

                        student.Courses.Add(contextCourse);
                    }

                    await ctx.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                MessageBox.Show(e.Message, "Saving error.");

                result = false;
            }

            return(result);
        }