Esempio n. 1
0
        public static StudentPerCourse AddStudentPerCourse()
        {
            StudentPerCourse spc = new StudentPerCourse();

            FrontEnd.PrintStudents();
            spc.StudentId = InputsMethods.SelectStudentId();
            FrontEnd.PrintCourses();
            spc.CourseId = InputsMethods.SelectCourseId();
            return(spc);
        }
Esempio n. 2
0
        public static void InsertStudentPerCourse(StudentPerCourse spc)
        {
            var connectionString = Properties.Settings.Default.connectionString;

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                string     query = @"INSERT INTO Student_Per_Course VALUES(@StudentId, @CourseId)";
                SqlCommand cmd   = new SqlCommand(query, con);

                //Pass values to Parameters
                cmd.Parameters.AddWithValue("@StudentId", spc.StudentId);
                cmd.Parameters.AddWithValue("@CourseId", spc.CourseId);

                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    Console.WriteLine("Records Inserted Successfully");
                    Menu.RunMenu();
                }
                catch (SqlException e) when(e.Number == 2627)  //Exception this relationship already exists
                {
                    Console.WriteLine("\n Wrong\n The Id already exists try again");
                    Inserts.InsertStudentPerCourse(Inserts.AddStudentPerCourse());
                }
                catch (SqlException e) when(e.Number == 547)  //Exception the id doesn't exist
                {
                    Console.WriteLine("\n Wrong\n The Id does not exist try again");
                    Inserts.InsertStudentPerCourse(Inserts.AddStudentPerCourse());
                }
                catch (SqlException e)
                {
                    Console.WriteLine("Error Generated. Details: " + e.ToString());
                }
                finally
                {
                    con.Close();
                    Console.ReadKey();
                }
            }
        }