views:

103

answers:

3

I have an ObjectDataSource with the proper SelectMethod and SelectParameters configured. The data source is bound to a grid view which successfully displays the data on page load.

What I need is the ability to rerun the Select method defined by the ObjectDataSource to be stored in a variable and manipulate the items in it. The issue I keep encountering is that calling the .Select() method always returns 0 rows despite it populating the grid view properly.

Is there a reason I can't manually rerun the Select() method on the object data source?

Update 2:

Here is how I setup the ObjectDataSource:

myObjectDataSource.TypeName = typeof(MyDataAccessObject).ToString();
myObjectDataSource.SelectMethod = "GetBy" + stringVariable;
myObjectDataSource.SelectCountMethod = "GetCountBy" + stringVariable;
myObjectDataSource.EnablePaging = true;

Update 1:

I run the Select() on a link button's OnClick event:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    SetupDataSource(); // populates the objSource's SelectMethod, SelectParameters and TypeName etc.
    var items = objSource.Select();
    int count = items.Count(); // returns 0;
}

The ObjectDataSource is setup (SelectMethod and SelectParameters are set) in the Page_Load event.

ObjectDataSource definition:

<asp:ObjectDataSource ID="objSource" runat="server" EnablePaging="True" SortParameterName="sortExpression" ></asp:ObjectDataSource>

GridView definition:

        <asp:GridView 
        ID="myGridView" 
        runat="server" 
        DataSourceID="objSource"
        AllowPaging="true"
        ShowHeader="true" 
        AutoGenerateColumns="false"
        AllowSorting="true" 
        Width="100%" >
A: 

You don't mention when (in what event you are trying to rebind the to the DataSource) Short code snippet may help.

If you are in Postback then Gridviews are not re-bound on postback, their rows are pulled back from viewstate. Resetting the gridview's DatasourceID to the object data source ID on page load (or init?) will cause the gridview to be rebound.

Roadie57
I'm not trying to rebind to the data source. I want to use the data source to rerun the select but store the results in a variable.
Baddie
A: 

I had similar problem before. I think .Select() is implemented using DataReader and once Select has been called once the reader is empty so any subsequent call to .Select or .Count() will return empty result.

Therefore, what you can do is to use .ToList() to store the result in a list and then just keep reusing the list.

oykuo
I can't store it in a list b/c queries sometimes return 300k+ rows.
Baddie
Excuse the last comment, I meant that I don't want to `always` store it in a variable because I don't always need it stored in a variable (only need it when a user clicks a button on the website).
Baddie
A: 

Turns out that underlying data access object's method took pagination into account and was returning .Take(maximumRows) which was always 0.

As a workaround, I programmatically disabled pagination via myObjectDataSource.EnablePaging = false; then created a new function which does not take into account pagination (a new function was required because the ObjectDataSource was looking for a function with specific paremeters).

Baddie