Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:40:34+00:00 2026-05-26T06:40:34+00:00

I am using subsonic and a gridview with objectdatasource (I have not used an

  • 0

I am using subsonic and a gridview with objectdatasource (I have not used an objectdatasource before)

The grid binds just fine, my only problem is that I don’t want to keep calling the database to get a count of all the records in the table. So when I call “FetchCount” (the SelectCountMethod) the first time I want to store it ina hiddenfield (or viewstate). For some reason my hiddenfield is always null when I try and access it, not the value, the actual hidden field. This is also the case if I try storing it in the viewstate.

This is my aspx. Just a gridview, ObjectDatasource and a hiddenfield

    <asp:GridView ID="grdResults" runat="server" AllowPaging="True"  AutoGenerateColumns="false"
 DataSourceID="PropertiesDataSource" PageSize="10" >
 <Columns >
 <asp:BoundField DataField="PropertyName" />
 </Columns>
 </asp:GridView>

<asp:ObjectDataSource ID="PropertiesDataSource" runat="server" SelectMethod="FetchPagedData" 
TypeName="TestWebsite.Usercontrols.Search.SearchResults" SelectCountMethod="FetchCount" 
StartRowIndexParameterName="start" 
MaximumRowsParameterName="pageLength" EnablePaging="True" />

<asp:HiddenField ID="hdnTotalRecords" runat="server" />

TypeName=”TestWebsite.Usercontrols.Search.SearchResults” is the namespace of the webpage that the above controls are on.

 public int? TotalRecords
    {
        get
        {
            if (hdnTotalRecords.Value != string.Empty) return int.Parse(hdnTotalRecords.Value);
            else return null;
        }
        set { hdnTotalRecords.Value = value.ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
             grdResults.DataBind();

    }

    [DataObjectMethod(DataObjectMethodType.Select, false)]
    public PropertyXCollection FetchPagedData(int start, int pageLength)
    { 
        int startIndex;
        if (start == 0)
            startIndex = 1;
        else           
            startIndex = start / pageLength + 1;

        PropertyXCollection collection = GetProperties(startIndex, 10);
        return collection;
    }

    public int FetchCount()
    {
        int returnVal = 0;
        if (TotalRecords != null)
            returnVal = (int)TotalRecords;
        else
        {
            TotalRecords = GetProperties(null, null).Count;
            returnVal = (int)TotalRecords;
        }
        return (int)returnVal;
    }

    PropertyXCollection GetProperties(int? pageIndex, int? pageCount)
    {
        //method that uses subsonic to return a collection of Properties from the database //and passes in the page index and count
    }

.

What am I doing wrong?

MY SOLUTION

I used the session instead

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:40:35+00:00Added an answer on May 26, 2026 at 6:40 am

    That’s because you’re trying to use SearchResults as both a Page and the underlying class backing up the ObjectDataSource.

    As explained in the documentation, since your data object methods are not static, the ObjectDataSource will create a new instance of the SearchResults class and call the methods on that instance. This means the hidden field will always be null and the view state contents will be meaningless.

    You can persist the record count in the ASP.NET session state instead, which you can access from the current HTTP context:

    public int? TotalRecords
    {
        get {
            return (int?) HttpContext.Current.Session["TotalRecords"];
        }
        set {
            HttpContext.Current.Session["TotalRecords"] = value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SubSonic 3.0.0.3 (the ActiveRecord approach) in an HttpHandler that I have
I'm using SubSonic 2.2 and sqlite and have encountered a problem when dealing with
Using SubSonic v2.3 in a web app. Each client (200+) will have their own
I'm using Subsonic 3.0.0.3 with ActiveRecord, running against a MySQL database. I'm finding that
I'm using Subsonic (simplerepository) and SQLite, and I have a class with an Int64
I am using SubSonic 2.1 and entcountered a problem while executing a Transaction with
Using SubSonic 3 ActiveRecord, I generated code from an existing database that had foreign
im using subsonic 3.0.0.3 activerecord and everything is fine and i get no error
Using Subsonic 3 I have the fields: o CreatedOn (datetime) o CreatedBy (nvarchar(50)) o
I've just started using Subsonic 2.2 and so far very impressed - think it'll

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.