ASP C#关闭阅读器时无效尝试调用FieldCount

时间:2016-05-12 19:54:27

标签: c# asp.net

我有一个函数(PopulateStudents),它使用数据库的结果填充下拉列表。在两种不同的情况下调用此函数:提供具有id的查询字符串时,以及onselectedindexchanged事件。基本上,如果从URL提供ID,则获取学生ID的数据库记录,其中包含semesterID和courseID。然后,根据studentID填充所有DDL。

如果URL中没有ID,则用户从DDL中选择一个学期。然后根据学期DDL中选择的学期ID填充课程DDL。然后,基于所选择的课程填充学生DDL。

问题是PopulateStudent函数在查询字符串中没有提供ID时工作正常,用户必须选择学期然后选择课程。当在查询字符串中提供ID时,PopulateStudents函数不起作用。该函数抛出一个错误,上面写着Invalid attempt to call FieldCount when reader is closed.我的代码出了什么问题?

这是aspx.cs文件:

protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    GetStudentScores(Convert.ToInt32(Request.QueryString["id"]));

                    string connString;
                    connString = RetrieveConnectionString();

                    SqlConnection conn = new SqlConnection(connString);

                    try
                    {
                        using (conn)
                        {
                            conn.Open();
                            SqlCommand cmd = new SqlCommand("dbo.GetEnrolleeDetails", conn);
                            cmd.Parameters.Add("@enrollmentID", System.Data.SqlDbType.Int);
                            cmd.Parameters["@enrollmentID"].Value = Convert.ToInt32(Request.QueryString["id"]);

                            cmd.CommandType = System.Data.CommandType.StoredProcedure;

                            SqlDataReader reader = cmd.ExecuteReader();
                            reader.Read();

                            int semesterID = Convert.ToInt32(reader["semesterId"]);
                            int courseID = Convert.ToInt32(reader["courseId"]);

                            PopulateCourses(semesterID);
                            PopulateStudents(courseID);

                            DDSemester.SelectedValue = Convert.ToString(semesterID);
                            DDCourse.SelectedValue = Convert.ToString(courseID);
                            DDStudent.SelectedValue = Request.QueryString["ID"];
                        }
                    }
                    catch (Exception err)
                    {
                        lblStatus.Text = err.Message;
                    }
                    finally
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }

        protected void DDCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                PopulateStudents(Convert.ToInt32(DDCourse.SelectedValue));
            }
        }



        protected void DDStudent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                GetStudentScores(Convert.ToInt32(DDStudent.SelectedValue));
            }
        }

        protected void DDSemester_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                PopulateCourses(Convert.ToInt32(DDSemester.SelectedValue));
            }
        }

        private void PopulateCourses(int semesterID)
        {
            string connString;
            connString = RetrieveConnectionString();

            SqlConnection conn = new SqlConnection(connString);

            try
            {
                using (conn)
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("dbo.GetSemesterCourses", conn);
                    cmd.Parameters.Add("@semesterID", System.Data.SqlDbType.Int);
                    cmd.Parameters["@semesterID"].Value = semesterID;

                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DDCourse.DataSource = cmd.ExecuteReader();
                    DDCourse.DataTextField = "courseName";
                    DDCourse.DataValueField = "courseId";
                    DDCourse.DataBind();
                }
            }
            catch (Exception err)
            {
                lblStatus.Text = err.Message;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }

        private void PopulateStudents(int courseID)
        {
            string connString;
            connString = RetrieveConnectionString();

            SqlConnection conn = new SqlConnection(connString);

            try
            {
                using (conn)
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("dbo.CourseEnrollment", conn);
                    cmd.Parameters.Add("@courseId", System.Data.SqlDbType.Int);
                    cmd.Parameters["@courseId"].Value = courseID;

                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DDStudent.DataSource = cmd.ExecuteReader();
                    DDStudent.DataTextField = "fullName";
                    DDStudent.DataValueField = "enrollmentId";
                    this.DataBind();
                }
            }
            catch (Exception err)
            {
                lblStatus.Text = err.Message;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }

这是asp:

<asp:Label ID="Label1" runat="server" Text="Semester:"></asp:Label>
    <asp:DropDownList ID="DDSemester" runat="server" AutoPostBack="True" AppendDataBoundItems="true" DataSourceID="SqlDataSource1" DataTextField="semesterName" DataValueField="semesterId" OnSelectedIndexChanged="DDSemester_SelectedIndexChanged">       
                <asp:ListItem Text="Select a Semester"></asp:ListItem>
    </asp:DropDownList><br />

    <asp:Label ID="Label15" runat="server" Text="Course:"></asp:Label>
    <asp:DropDownList ID="DDCourse" runat="server" AutoPostBack="True" AppendDataBoundItems="true" OnSelectedIndexChanged="DDCourse_SelectedIndexChanged">
        <asp:ListItem Text="Select a Course"></asp:ListItem>
    </asp:DropDownList><br />

    <asp:Label ID="Label16" runat="server" Text="Student"></asp:Label>
    <asp:DropDownList ID="DDStudent" runat="server" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="DDStudent_SelectedIndexChanged">
        <asp:ListItem Text="Select a Student"></asp:ListItem>
    </asp:DropDownList><br />

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

PopulateCourses和PopulateStudents函数之间实际上有一点不同,我没有注意到。在课程中,我使用了DDCourse.DataBind(),在学生中我使用了this.DataBind()。

在populatestudents函数中,将this.DataBind()更改为DDStudent.DataBind()修复了我的问题。