2013-08-27 62 views
1

我正在面對我的程序出現問題,它運行它時出現此錯誤「DataBinding:'System.Data.DataRowView'不包含名稱爲' StudentName'。「,那麼如何解決?DataBinding:'System.Data.DataRowView'error

<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) 
     { } 
    } 
} 
+0

您需要向我們展示您的代碼。 –

+0

看來你的代碼在中間缺少一個部分。之後 paqogomez

+0

現在已完成 – Rose

回答

0
當你穿上斷點

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

是否CourseID有一個有效的整數值?還是0?

當你把一個斷點:

RepeaterDataVeiw.DataSource = dt;

是否dt有任何記錄?

而且您確定StudentNamedt中的數據行的屬性?

0

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作爲輸出列。