C# 我在关闭表单时收到{枚举数实例化后集合被修改}此异常

C# 我在关闭表单时收到{枚举数实例化后集合被修改}此异常,c#,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

我的代码如下:

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();
    }

欢迎来到SO!当你描述你的程序的意图,并把它减少到一个再现你的错误的最小的例子时,这将非常有帮助。同样,也是最重要的一点:到目前为止,你做了什么来重现错误?我只是没做什么。我使用了一些变量和数据集。我认为在关闭表格之前,这些需要处理?你说得对。但我无法克服。我们在表单关闭事件的某个地方运行了什么代码吗?它得到了解决……我使用了Application.ThreadExit();欢迎来到SO!当你描述你的程序的意图,并把它减少到一个再现你的错误的最小的例子时,这将非常有帮助。同样,也是最重要的一点:到目前为止,你做了什么来重现错误?我只是没做什么。我使用了一些变量和数据集。我认为在关闭表格之前,这些需要处理?你说得对。但我无法克服。我们在表单关闭事件的某个地方运行了什么代码吗?它得到了解决……我使用了Application.ThreadExit();