使用vb将数据绑定到asp.net中的gridview时出错

时间:2011-12-09 11:48:07

标签: asp.net asp.net-mvc

我收到以下错误,我无法解决。

<%@ Page Language="VB" MasterPageFile ="~/Master.master"  AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:GridView ID="GridView1"  PageSize ="50" runat="server" AllowPaging="True"    AllowSorting="True"
           AutoGenerateColumns="False" DataKeyNames="ID">
         <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:BoundField DataField="Dname" HeaderText="Player" SortExpression="Dname" />
             <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
             <asp:BoundField DataField="GTScore" HeaderText="Score" 
                 SortExpression="GTScore" />
          </Columns>
        </asp:GridView>
</asp:Content>

这是我的.aspx代码

 Partial Class test
 Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles     Me.Load
    Dim fDate As Date = New Date(Today.Year, Today.Month, 1)
    Dim tDate As Date = New Date(Today.Year, Today.Month, Date.DaysInMonth(Today.Year, Today.Month))
    GridView1.DataSource = Game.SelectToppersOfMonth1(fDate, tDate)
    GridView1.DataBind()
End Sub
End Class

这是我的.aspx.vb代码。我只是从函数SelectToppersOfMonth1调用存储过程

我存储过程中的select查询是

SELECT TOP 100 A.[dispName] as [Dname], A.[city] as [City], SUM(B.[score]) as [GTScore]

FROM [Players] A,[Games] B 

WHERE A.[ID]=B.[playerID] AND B.[startedOn] BETWEEN @fromDate AND @toDate 

GROUP BY A.[dispName], A.[city] ORDER BY [GTScore] DESC

我收到错误

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ID'.
Description: An unhandled exception occurred during the execution of the current web       request. Please review the stack trace for more information about the error and     where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView'  does not contain a property with the name 'ID'.

2 个答案:

答案 0 :(得分:2)

只需从页面标记DataKeyNames="ID"中删除它,因为您的查询未返回任何ID列并且会破坏绑定。或者修改您的SQL查询以返回ID列。

答案 1 :(得分:0)

您在DataKeyNames属性中引用ID,它不在您的SQL查询中。这是正常的吗?