开发者

Problem with ASP Repeater and SQL Data Source

开发者 https://www.devze.com 2023-01-20 14:56 出处:网络
Here\'s my data table in my SQL DB, and below that is the code for my Repeater control: ArticleID int NOT NULL,

Here's my data table in my SQL DB, and below that is the code for my Repeater control:

ArticleID int NOT NULL,
ArticleTitle varchar(100) NOT NULL,
ArticleCategory int NOT NULL,
ArticleDate datetime NOT NULL,
ArticleContent text NOT NULL,
DeletedYN char(3) NOT NULL

<asp:Repeater runat="server" ID="rptArticles" DataSourceID="dsLatestArticles">
    <HeaderTemplate>
        <span class="title"><%#Container.DataItem("ArticleTitle")%></span><br />
        <span class="title3">Posted On: <%#Container.DataItem("ArticleDate")%></span>
    </HeaderTemplate>
    <ItemTemplate><p><%#Container.DataItem("ArticleContent")%></p></ItemTemplate>
    <FooterTemplate>
        <p class="RightAlign"><a href="/Articles/Read.aspx?i=<%#Container.DataItem("ArticleID") %>">Read more...</a></p>
    </FooterTemplate>
</asp:Repeater>

When I try to load the page this is on, I get an error saying Object variable or With block variable not set.

For reference, my SqlDataSource is configured as follows:

<asp:SqlDataSource ID="dsLatestArticles" runat="server" 
    ConnectionString="<%$ ConnectionStrings:VBSiteConnectionString %>"        
    SelectCommand="SELECT [ArticleID], [ArticleTitle], [ArticleDate], [ArticleContent] FROM [Articles] WHERE ([DeletedYN] = @DeletedYN)">
    <SelectParameters>
        <asp:Parameter DefaultValue="No" Name="DeletedYN" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

I don't know what I've done wrong here, can anyone see a problem? For further reference, here's my Stack Trace:

[NullReferenceException: Object variable or With block variable not set.]
   Microsoft.VisualBasic.CompilerServices.Container..ctor(Object Instance) +510276
   Microsoft.VisualBasic.CompilerServices.NewLateBinding.InternalLateIndexGet(Object Instance, Object[] Arguments, String[] ArgumentNames, Boolean ReportErrors, ResolutionFailure& Failure) +88
   Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateIndexGet(Object Instance, Object[] Arguments, String[] ArgumentNames) +21
   ASP.default_aspx.__DataBind__control3(Object sender, EventArgs e) in D:\Development\Projects\Web\LogansArchive\Default.aspx:9
   System.Web.UI.Control.OnDataBinding(EventArgs e) +99
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
   System.Web.UI.Control.DataBind() +15
   System.Web.UI开发者_运维问答.Control.DataBindChildren() +211
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +124
   System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +323
   System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51
   System.Web.UI.WebControls.Repeater.DataBind() +75
   System.Web.UI.WebControls.Repeater.EnsureDataBound() +55
   System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +15
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

Thanks in advance!


In case someone else comes across this problem, here's a solution that I've found

The Cause
I was pulling data from my SqlDataSource in the <HeaderTemplate> section of my Repeater control. This is what caused the problem. I'm not sure why, but the point is that doing this will not work.

The Solution
All data retrieval (i.e. <%#Container.DataItem("Field1") %> block) must be used in the <ItemTemplate> section of the Repeater control.

0

精彩评论

暂无评论...
验证码 换一张
取 消