数据绑定:';System.Data.DataRowView';错误

本文关键字:DataRowView 错误 Data System 数据绑定 | 更新日期: 2023-09-27 18:29:16

我的程序遇到问题,运行它时出现错误"DataBinding:'System.Data.DataRowView'不包含名为'StudentName'的属性。",那么如何修复它呢??

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderSubMenu" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderMainContent" Runat="Server">
    <table>
        <tr>
            <td style="width:150px;">Courses</td>
            <td style="width:20px">:</td>
            <td>
                <asp:DropDownList ID="DropDownListCourses" runat="server"></asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="width:150px;"></td>
            <td style="width:20px"></td>
            <td>
                <asp:Button ID="ButtonView" runat="server" Text="Veiw"
                onclick="ButtonView_Click"/>
            </td>
        </tr>
    </table>
    <div style="padding-top:30px"></div>
    <table>
    <asp:Repeater ID="RepeaterDataVeiw" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <td>Student Name</td>
                    <td>Course</td>
                    <td>Subject</td>
                    <td>Veiw</td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
                <tr>
                    <td><%#Eval("StudentName")%></td>
                    <td><%#Eval("CourseName")%></td>
                    <td><%#Eval("SubjectName")%></td>
                    <td><a href="ListForum.aspx">Forum</a></td>
                </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
                <tr>
                    <td><%#Eval("StudentName")%></td>
                    <td><%#Eval("CourseName")%></td>
                    <td><%#Eval("SubjectName")%></td>
                    <td><a href="ListForum.aspx">Forum</a></td>
                </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    </table>
</asp:Content>

代码背后:

public partial class Student_ListCourses : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                System.Data.DataTable  dt;
                BusinessLayer.CoursesController oCourse = new BusinessLayer.CoursesController();
                dt = oCourse.Select();
                DropDownListCourses.DataSource = dt;
                DropDownListCourses.DataValueField = "CourseID";
                DropDownListCourses.DataTextField = "CourseName";
                DropDownListCourses.DataBind();
            }
        }
    }
    protected void ButtonView_Click(object sender, EventArgs e)
    {
        try
        {
            System.Data.DataTable dt;
            BusinessLayer.SubjectsController oCourse = new       BusinessLayer.SubjectsController();
            oCourse.CourseID = int.Parse(DropDownListCourses.SelectedValue);
            dt = oCourse.Select();
            RepeaterDataVeiw.DataSource = dt;
            RepeaterDataVeiw.DataBind();
        }
        catch (Exception ex)
        { }
    }
}

数据绑定:';System.Data.DataRowView';错误

当您在上设置断点时

oCourse.CourseID = int.Parse(DropDownListCourses.SelectedValue);

CourseID是否具有有效的整数值?还是0?

当你在上设置断点时

RepeaterDataVeiw.DataSource = dt;

dt有记录吗?

你确定StudentNamedt中数据行的一个性质吗?

oCourse.Select()返回的dt很可能没有名为StudentName的DataColumn。

例如,您可以替换:

BusinessLayer.SubjectsController oCourse = new       BusinessLayer.SubjectsController();
oCourse.CourseID = int.Parse(DropDownListCourses.SelectedValue);
dt = oCourse.Select();

带有:

dt = new DataTable();
dt.Columns.Add("StudentName");
dt.Columns.Add("CourseName");
dt.Columns.Add("SubjectName");
dt.Rows.Add("Test student name", "Test course name", "Test subject name");

并且您的页面将成功加载(尽管只是显示了测试数据)。

RepeaterDataVeiw.DataSource = dt处设置一个断点,并检查dt.Columns的值。还要返回并查看BusinessLayer.SubjectsControllerSelect是否包含StudentName作为输出列。