簡體   English   中英

使用vb將數據綁定到asp.net中的gridview時出錯

[英]Error while binding data to gridview in asp.net using vb

我得到以下錯誤,我無法解決它。

<%@ 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 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'.

只需從頁面標記中刪除它: DataKeyNames="ID"因為您的查詢未返回任何ID列並且會破壞綁定。 或者修改SQL查詢以返回ID列。

您在DataKeyNames屬性中引用ID,它不在您的SQL查詢中。 這是正常的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM