Пример #1
0
        public int AddToTempGrid(System.Windows.Forms.DataGridView dgvTemp, Student student)
        {
            int newRowIndex = dgvTemp.Rows.Add();

            dgvTemp.Rows[newRowIndex].Cells["ID"].Value = student.ID;
            dgvTemp.Rows[newRowIndex].Cells["StudentName"].Value = student.Name;
            dgvTemp.Rows[newRowIndex].Cells["DOB"].Value = student.DateOfBirth.ToString("dd-MMMM-yyyy");
            dgvTemp.Rows[newRowIndex].Cells["GradePointAvg"].Value = student.GradePointAverage.ToString("0.00");
            dgvTemp.Rows[newRowIndex].Cells["Active"].Value = (student.IsActive) ? "Yes" : "No";
            dgvTemp.Rows[newRowIndex].Cells["Saved"].Value = 0;

            return newRowIndex;
        }
Пример #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            int recordsCount = RecordsToBeSavedCount();
            int savedRecordsCount = 0;

            if (recordsCount > 0)
            {
                foreach (DataGridViewRow studentRow in dgvTempStudents.Rows)
                {
                    if (studentRow.Cells["Saved"].Value.ToString() == "0")
                    {
                        Student student = new Student();
                        student.Name = studentRow.Cells["StudentName"].Value.ToString();
                        student.DateOfBirth = DateTime.Parse(studentRow.Cells["DOB"].Value.ToString());
                        student.GradePointAverage = Decimal.Parse(studentRow.Cells["GradePointAvg"].Value.ToString());
                        student.IsActive = (studentRow.Cells["Active"].Value.ToString().ToLower() == "yes") ? true : false;

                        try
                        {
                            if (studentRegister.RegisterStudent(student))
                            {
                                savedRecordsCount++;
                                studentRow.Cells["Saved"].Value = "1";
                                dgvTempStudents.Rows[studentRow.Index].DefaultCellStyle.BackColor = Color.LightGreen;
                            }
                            else
                                dgvTempStudents.Rows[studentRow.Index].DefaultCellStyle.BackColor = Color.MistyRose;
                        }
                        catch (Exception exp)
                        {
                            string message = "An error occured while saving...\n\n" + exp.Message;
                            MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                    }
                }

                if (savedRecordsCount > 0)
                {
                    string message = savedRecordsCount + " out of " + recordsCount + " record(s) were saved successfully!";
                    MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("No records were saved!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            else
                MessageBox.Show("No new records to save!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
Пример #3
0
        public bool RegisterStudent(Student student)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "SP_RegisterNewStudent";
                cmd.Parameters.AddWithValue("@Name", student.Name);
                cmd.Parameters.AddWithValue("@DateOfBirth", student.DateOfBirth);
                cmd.Parameters.AddWithValue("@GradePointAverage", student.GradePointAverage);
                cmd.Parameters.AddWithValue("@Active", student.IsActive);
                // ID is not required, it will be auto inserted. Student.ID is only for viewing purpose.

                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch(Exception exp)
                {
                    if (con.State != System.Data.ConnectionState.Closed)
                        con.Close();
                    throw exp;
                }
            }

            return true;
        }