예제 #1
0
        private void createEvent_Click(object sender, EventArgs e)
        {
            //sets the time start and end of the event
            string   timeStart    = " " + eventStartTime.Value + ":" + eventStartMin.Value + ":00 " + startComboBox.Text;
            DateTime timeStartSet = Convert.ToDateTime(timeStart);


            string   timeEnd    = " " + eventEndHour.Value + ":" + eventEndMin.Value + " " + endComboBox.Text;
            DateTime timeEndSet = Convert.ToDateTime(timeEnd);

            //sets dateEnd
            DateTime dateEnd     = datePicker.Value;
            string   dateEndOnly = dateEnd.ToString("MM-dd-yyyy");
            string   timeEnd2    = " " + eventEndHour.Value + ":" + eventEndMin.Value + " " + endComboBox.Text;

            dateAndTimeEnd = Convert.ToDateTime(dateEndOnly + timeEnd2);
            //set DateStart
            DateTime dateStart     = DateTime.Now;
            string   dateStartOnly = dateStart.ToString("MM-dd-yyyy");
            string   timeStart2    = " " + eventEndHour.Value + ":" + eventEndMin.Value + " " + endComboBox.Text;

            dateAndTimeStart = Convert.ToDateTime(dateStartOnly + timeStart2);
            //checks if time input is valid
            if (dateAndTimeStart > dateAndTimeEnd)
            {
                MessageBox.Show("Invalid event time.");
            }
            else
            {
                SqlConnection connection = new SqlConnection(source);

                //checks if there is an existing table
                string     selectTable = "Select * from INFORMATION_SCHEMA.TABLES where table_name = '" + eventNameTxtBox.Text + "'";
                SqlCommand checkTable  = new SqlCommand(selectTable, connection);
                connection.Open();
                SqlDataReader readTable = checkTable.ExecuteReader();
                if (readTable.Read())
                {
                    MessageBox.Show(eventNameTxtBox.Text + " already exists.");
                    connection.Close();
                }
                else
                {
                    //if there's no existing table, create new table
                    connection.Close();
                    string eventNameText = Convert.ToString(eventNameTxtBox.Text);
                    connection.Open();
                    string table = @"Create table [" + eventNameTxtBox.Text + "](StudentID varchar(50) primary key, StudentName varchar(50), Course varchar(50), Block varchar(50), Attendance varchar(50),TimeIn time(7),)";
                    currentEvent = eventNameTxtBox.Text;
                    SqlCommand createTable = new SqlCommand(table, connection);
                    try
                    {
                        createTable.ExecuteNonQuery();
                        connection.Close();
                    }
                    catch
                    {
                    }
                    //copy data from StudentInfo to Event
                    connection.Close();
                    connection.Open();
                    SqlCommand copyInfo = new SqlCommand("Insert into [" + eventNameTxtBox.Text + "] (StudentID, StudentName, Course, Block)" + "Select StudentID#, Name, Course, Block from dbo.StudentData", connection);
                    try
                    {
                        copyInfo.ExecuteNonQuery();
                    }
                    catch
                    {
                    }
                    connection.Close();

                    //inserts event info
                    connection.Open();
                    string insertIntoEvents = "Insert into dbo.Events (EventName, TimeStart, TimeEnd, Date) " + "Values(@EventName, @TimeStart, @TimeEnd, @Date)";
                    using (SqlCommand insertEvents = new SqlCommand(insertIntoEvents, connection))
                    {
                        insertEvents.Parameters.AddWithValue("@EventName", eventNameTxtBox.Text);
                        insertEvents.Parameters.AddWithValue("@TimeStart", timeStartSet);
                        insertEvents.Parameters.AddWithValue("@TimeEnd", timeEndSet);
                        insertEvents.Parameters.AddWithValue("@Date", DateTime.Now);

                        int insert = insertEvents.ExecuteNonQuery();
                        connection.Close();
                    }
                    connection.Close();
                    MessageBox.Show("Event successfully created!");
                    this.Hide();
                    registration registration = new registration();
                    registration.Show();
                }
            }
        }