关闭表单时,我收到{在枚举器实例化后修改了集合}此异常

时间:2016-01-28 06:11:43

标签: c#

我的代码是这样的:

public partial class TimeTableForm : Form
{
    string text;
    string t;
    string day;
    public TimeTableForm(string str, string element,string d)
    {
        InitializeComponent();
        text = str;
        t = element;
        day = d;
    }


    private void TimeTableForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        //timetableData.Dispose();

        //this.Visible = false;

        this.Dispose();
        Application.Exit();

        //SelectElementForm ob = new SelectElementForm();
        //ob.Show();
        //this.Close();
    }


    //to generate the time table
    public void GenerateTimeTable()
    {

        if(t == "Classes")
        {
            LogFile file = new LogFile();
            file.LoggingInfo("Class selected: "+text+" and day selected: "+day);
            SqlConnection conn = new SqlConnection("Data Source=WONDERBIZ;Initial Catalog=School_TimeTable;Integrated Security=True");
            conn.Open();
            string query = "select p.periodTime,s.subjectName,tc.teacherName,cr.classRomId from tbl_timetable t"
                            +" join tbl_Days d on d.dayId = t.dayId"
                            +" join tbl_Periods p on p.periodId = t.periodId"
                            +" join tbl_Subjects s on s.subjectId = t.subjectId"
                            +" join tbl_teachers tc on tc.teacherId = t.teacherId"
                            +" join tbl_classes cl on cl.classId = t.classId"
                            +" join tbl_classrooms cr on cr.classRomId = t.classroomId"
                            +" where cl.className = '"+text+"'"
                            +" and d.dayName = '"+day+"'"
                            +" order by p.periodId asc;";
            SqlCommand cmd = new SqlCommand(query, conn);
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                //filling up the dataset
                da.Fill(ds, "timetable data");

                //showing the data
                timetableData.DataSource = ds;
                timetableData.DataMember = "timetable data";
                conn.Close();
                //ds.Clear();

            }
            catch(SqlException ex)
            {
                file.LoggingError(t + " timetable:" + ex.Message);
            }
            catch(Exception ex)
            {

                file.LoggingError(t+" timetable:" + ex);
            }
            ElementLabel.Text = "Class:";
            SelectedElementLabel.Text = text;
            dayLabel.Text = day;
        }

        if (t == "Teachers")
        {
            LogFile file = new LogFile();
            file.LoggingInfo("Teacher selected: " + text + " and day selected: " + day);
            SqlConnection conn = new SqlConnection("Data Source=WONDERBIZ;Initial Catalog=School_TimeTable;Integrated Security=True");
            conn.Open();
            string query = "select cl.className,p.periodTime,s.subjectName,cr.classRomId from tbl_timetable t"
                            + " join tbl_Days d on d.dayId = t.dayId"
                            + " join tbl_Periods p on p.periodId = t.periodId"
                            + " join tbl_Subjects s on s.subjectId = t.subjectId"
                            + " join tbl_teachers tc on tc.teacherId = t.teacherId"
                            + " join tbl_classes cl on cl.classId = t.classId"
                            + " join tbl_classrooms cr on cr.classRomId = t.classroomId"
                            + " where tc.teacherName = '" + text + "'"
                            + " and d.dayName = '" + day + "'"
                            + " order by p.periodId asc;";
            SqlCommand cmd = new SqlCommand(query, conn);
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                //filling up the dataset
                da.Fill(ds, "timetable data");

                //showing the data
                timetableData.DataSource = ds;
                timetableData.DataMember = "timetable data";
                conn.Close();

            }
            catch (SqlException ex)
            {
                file.LoggingError(t + " timetable:" + ex.Message);
            }
            catch (Exception ex)
            {

                file.LoggingError(t + " timetable:" + ex);
            }
            ElementLabel.Text = "Teacher:";
            SelectedElementLabel.Text = text;
            dayLabel.Text = day;
        }

        if (t == "Classrooms")
        {
            LogFile file = new LogFile();
            file.LoggingInfo("Classroom selected: " + text + " and day selected: " + day);
            SqlConnection conn = new SqlConnection("Data Source=WONDERBIZ;Initial Catalog=School_TimeTable;Integrated Security=True");
            conn.Open();
            string query = "select cl.className,p.periodTime,s.subjectName,tc.teacherName from tbl_timetable t"
                            + " join tbl_Days d on d.dayId = t.dayId"
                            + " join tbl_Periods p on p.periodId = t.periodId"
                            + " join tbl_Subjects s on s.subjectId = t.subjectId"
                            + " join tbl_teachers tc on tc.teacherId = t.teacherId"
                            + " join tbl_classes cl on cl.classId = t.classId"
                            + " join tbl_classrooms cr on cr.classRomId = t.classroomId"
                            + " where cr.classRomId = '" + text + "'"
                            + " and d.dayName = '" + day + "'"
                            + " order by p.periodId asc;";
            SqlCommand cmd = new SqlCommand(query, conn);
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                //filling up the dataset
                da.Fill(ds, "timetable data");

                //showing the data
                timetableData.DataSource = ds;
                timetableData.DataMember = "timetable data";
                conn.Close();

            }
            catch (SqlException ex)
            {
                file.LoggingError(t + " timetable:" + ex.Message);
            }
            catch (Exception ex)
            {

                file.LoggingError(t + " timetable:" + ex);
            }
            ElementLabel.Text = "Classroom:";
            SelectedElementLabel.Text = text;
            dayLabel.Text = day;
        }
    }

    private void TimeTableForm_Load(object sender, EventArgs e)
    {
        GenerateTimeTable();
    }

0 个答案:

没有答案