private void toolStripButtonSave_Click(object sender, EventArgs e)
 {
     using (SchoolJournalEntities context = new SchoolJournalEntities())
     {
         var IDs = from user in context.Users select user.UserID;
         for (int i = 0; i < dataGridStudents.Rows.Count - 1; i++)
         {
             if (!IDs.Contains(int.Parse(dataGridStudents["_StudentID", i].Value.ToString())))
             {
                 try
                 {
                     AddNewStudent(i);
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("Error while updating student!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
             }
             else
             {
                 try
                 {
                     DateTime dateOfJoin;
                     DateTime?parsedDateOfJoin = null;
                     bool     success          = DateTime.TryParse(dataGridStudents["_Date_of_join", i].Value.ToString(), out dateOfJoin);
                     if (success)
                     {
                         parsedDateOfJoin = dateOfJoin;
                     }
                     //check all fields and save
                     StudentDAL.UpdateStudent(new StudentInfo(
                                                  int.Parse(dataGridStudents["_StudentID", i].Value.ToString()),
                                                  dataGridStudents["_First_name", i].Value.ToString(),
                                                  dataGridStudents["_Last_name", i].Value.ToString(),
                                                  dataGridStudents["_Patronymic", i].Value.ToString(),
                                                  null,
                                                  dataGridStudents["_Email", i].Value.ToString(),
                                                  dataGridStudents["_Password", i].Value.ToString(),
                                                  null,
                                                  0, //заглушка для gradeID
                                                  dataGridStudents["_Grade", i].EditedFormattedValue.ToString(),
                                                  parsedDateOfJoin));
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("Error while updating student!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
             }
         }
         UpdateDataGridViews();
     }
 }
        private void dataGridStudents_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            int rowIndex = e.Row.Index - 1;

            using (SchoolJournalEntities context = new SchoolJournalEntities())
            {
                int lastID = 0;
                try
                {
                    lastID = Math.Max(
                        int.Parse(dataGridStudents["_StudentID", dataGridStudents.Rows.Count - 3].Value.ToString()),
                        (int)(from u in context.Users select u.UserID).Max());
                }
                catch
                { }
                dataGridStudents["_StudentID", rowIndex].Value = lastID + 1;
            }
        }
Esempio n. 3
0
        private void saveGroups()
        {
            SchoolJournalEntities context = new SchoolJournalEntities();
            var groupIDs = from g in context.Groups select g.GroupID;

            try
            {
                foreach (DataGridViewRow r in GroupsDataGridView.Rows)
                {
                    if (r.IsNewRow)
                    {
                        continue;
                    }
                    int groupID = int.Parse(GroupsDataGridView["GroupID", r.Index].Value.ToString());
                    if (groupIDs.Contains(groupID)) //update
                    {
                        GroupDAL.UpdateGroup(groupID,
                                             int.Parse(GroupsDataGridView["TeacherID", r.Index].Value.ToString()),
                                             GroupsDataGridView["Subject", r.Index].EditedFormattedValue.ToString(),
                                             GroupsDataGridView["Grade", r.Index].EditedFormattedValue.ToString(),
                                             GroupsDataGridView["Description", r.Index].Value.ToString());
                    }
                    else //add new
                    {
                        GroupsDataGridView["GroupID", r.Index].Value =
                            GroupDAL.NewGroup(int.Parse(GroupsDataGridView["TeacherID", r.Index].Value.ToString()),
                                              GroupsDataGridView["Subject", r.Index].EditedFormattedValue.ToString(),
                                              GroupsDataGridView["Grade", r.Index].EditedFormattedValue.ToString(),
                                              GroupsDataGridView["Description", r.Index].Value.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                context.Dispose();
            }
        }
        private void toolStripButtonSaveChanges_Click(object sender, EventArgs e)
        {
            SchoolJournalEntities context = null;

            try
            {
                context = new SchoolJournalEntities();
                var teacherIDs = from teacher in context.Teachers select teacher.TeacherID;


                foreach (DataGridViewRow r in teachersListDataGridView.Rows)
                {
                    if (r.IsNewRow)
                    {
                        continue;
                    }

                    int teacherID = -1;
                    try
                    {
                        teacherID = int.Parse(teachersListDataGridView["teacherID", r.Index].Value.ToString());
                    }
                    catch (ArgumentException)
                    {
                        MessageBox.Show(
                            string.Format("Incorrect teacherID in row {0}!\nMaybe you've created empty rows?", r.Index),
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        continue;
                    }

                    if (teacherIDs.Contains(teacherID)) //update record
                    {
                        DateTime?dateOfBirth;
                        try
                        {
                            dateOfBirth = DateTime.Parse(teachersListDataGridView["dateOfBirth", r.Index].Value.ToString());
                        }
                        catch
                        {
                            dateOfBirth = null;
                        }
                        TeacherDAL.UpdateTeacher(new TeacherInfo(
                                                     teacherID,
                                                     teachersListDataGridView["firstName", r.Index].Value.ToString(),
                                                     teachersListDataGridView["lastName", r.Index].Value.ToString(),
                                                     teachersListDataGridView["patronymic", r.Index].Value.ToString(),
                                                     dateOfBirth,
                                                     teachersListDataGridView["email", r.Index].Value.ToString(),
                                                     teachersListDataGridView["password", r.Index].Value.ToString(),
                                                     teachersListDataGridView["phone", r.Index].Value.ToString(),
                                                     teachersListDataGridView["specialization", r.Index].Value.ToString(),
                                                     teachersListDataGridView["category", r.Index].Value.ToString())
                                                 );
                    }
                    else //add new record
                    {
                        DateTime?dateOfBirth;
                        try
                        {
                            dateOfBirth = DateTime.Parse(teachersListDataGridView["dateOfBirth", r.Index].Value.ToString());
                        }
                        catch
                        {
                            dateOfBirth = null;
                        }
                        teachersListDataGridView["teacherID", r.Index].Value =
                            TeacherDAL.AddNewTeacher(new TeacherInfo(
                                                         -1, //заглушка
                                                         teachersListDataGridView["firstName", r.Index].Value.ToString(),
                                                         teachersListDataGridView["lastName", r.Index].Value.ToString(),
                                                         teachersListDataGridView["patronymic", r.Index].Value.ToString(),
                                                         dateOfBirth,
                                                         teachersListDataGridView["email", r.Index].Value.ToString(),
                                                         teachersListDataGridView["password", r.Index].Value.ToString(),
                                                         teachersListDataGridView["phone", r.Index].Value.ToString(),
                                                         teachersListDataGridView["specialization", r.Index].Value.ToString(),
                                                         teachersListDataGridView["category", r.Index].Value.ToString())
                                                     );
                    }
                }

                this.toolStripSavedStatus.Text = "Changes saved!";
                MessageBox.Show("Changes successfully changed!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                string innerException = string.Empty;
                if (ex.InnerException != null)
                {
                    innerException = ex.InnerException.Message;
                }
                MessageBox.Show(ex.Message + "\n" + innerException, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }