为什么我的c#数据库循环没有向gridview添加数据?

时间:2016-03-04 07:48:56

标签: c# asp.net

我有一个asp c#方法,它应该将数据集绑定到我的aspx文件中的gridview。

现在,我需要向网格添加一个自定义列,因为它的值是根据某个数据库列的值在另一个类中计算的。因此,我使用带有while循环的reader来循环遍历结果,然后单独添加列。但是不显示网格。这是我现在使用的代码:

protected void Page_Load(object sender, EventArgs e)
        {
            string connstring;
            connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);
            SqlCommand cmd = new SqlCommand("dbo.EnrolledStudents", conn);

            SqlDataReader reader;

            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();

                DataSet ds = new DataSet();
                DataTable dt = new DataTable("Students");
                ds.Tables.Add("Students");
                while (reader.Read())
                {
                    Grades studentGrades = new Grades();
                    studentGrades.percentage = Convert.ToInt32(reader["percentage"]);
                    string letterGrade = studentGrades.calculate();
                    DataRow dr = dt.NewRow();

                    dt.Columns.Add(new DataColumn("studentId", typeof(int)));
                    dt.Columns.Add(new DataColumn("firstName", typeof(string)));
                    dt.Columns.Add(new DataColumn("lastName", typeof(string)));
                    dt.Columns.Add(new DataColumn("courseName", typeof(string)));
                    dt.Columns.Add(new DataColumn("season", typeof(string)));
                    dt.Columns.Add(new DataColumn("year", typeof(string)));
                    dt.Columns.Add(new DataColumn("letterGrade", typeof(string)));

                    dr["studentId"] = reader["studentId"];
                    dr["firstName"] = reader["firstName"];
                    dr["lastName"] = reader["lastName"];
                    dr["courseName"] = reader["courseName"];
                    dr["season"] = reader["season"];
                    dr["year"] = reader["year"];
                    dr["letterGrade"] = letterGrade;
                    dt.Rows.Add(dr);
                }
                ds.Tables.Add(dt);

                StudentGrid.DataSource = ds;
                StudentGrid.DataBind();
                reader.Close();
            }
            catch (Exception err)
            {
                DBError.Text = "Error getting database records...";
                DBError.Text += err.Message;
            }

        }

  }

这是我的aspx文件:

<h1>Student List</h1>
        <asp:SqlDataSource ID="GetStudents" runat="server" ConnectionString="<%$ ConnectionStrings:dbConn %>" SelectCommand="dbo.GetStudents" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        <asp:GridView ID="StudentGrid" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowSelectButton="False" />
                <asp:BoundField DataField="studentId" HeaderText="ID" />
                <asp:BoundField DataField="firstName" HeaderText="First Name" />
                <asp:BoundField DataField="lastName" HeaderText="Last Name" />
                <asp:BoundField DataField="courseName" HeaderText="Course Name" />
                <asp:BoundField DataField="season" HeaderText="Semester" />
                <asp:BoundField DataField="year" HeaderText="Year" />
                <asp:BoundField DataField="letterGrade" HeaderText="Grade" />
            </Columns>
        </asp:GridView>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/AddScore.aspx">Add Score</asp:HyperLink>
        <asp:TextBox ID="DBError" runat="server" Visible="false"></asp:TextBox>

在我的DBError标签中,我收到消息Object无法从DBNull转换为其他类型。知道为什么吗?

0 个答案:

没有答案