开发者

Object data source select method returning nothing in code call

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%" >


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).


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜