locked
ObjectDataSource.SelectParameters.Clear causes exception RRS feed

  • Question

  • User260076833 posted

    Hello,

    I have a ListView connected to an ObjectDataSource.

    Whenever the user changes the values of some controls, I update this data source as follows:

            private void adaptDataSource()
            {
                myDataSource.SelectMethod = "GetDataByMatch";
    
                myDataSource.SelectParameters.Clear();
                myDataSource.SelectParameters.Add("par1", val1);
                myDataSource.SelectParameters.Add("par2", val2);
                myDataSource.SelectParameters.Add("par3", val3);
            }
    

    This always worked when calling adaptDataSource in the change event of some controls.

    However, now I want to also call it within the OnPreRender event of the ListView's DataPager.

    Then, when reaching the instruction myDataSource.SelectParameters.Clear();, I get an exception:

    could not find a non-generic method 'GetDataByMatch' that has no parameters. 

    This is correct, the method 'GetDataByMatch' needs parameters. But I want to set those parameters after clearing them.

    How can I set the parameters without causing the exception?

    Thanks
    Yeoman

    Wednesday, January 20, 2016 10:34 AM

Answers

  • User-271186128 posted

    Hi Yeoman,

    However, now I want to also call it within the OnPreRender event of the ListView's DataPager.

    Then, when reaching the instruction myDataSource.SelectParameters.Clear();, I get an exception:

    From your description, I'm confused about that why you want to change the ObjectDataSource Select Method in the OnPreRender event of the ListView's DataPager. 

    The DataPager is in the ListView, when you bind the ListView, it will provide paging functionality according the ObjectDataSource.

    If you want to change the ObjectDataSource select method, I suggest you could use the change event of some controls or the ListView, but not suggest you use the OnPreRender event of the ListView's DataPager.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 21, 2016 5:56 AM
  • User-271186128 posted

    Hi Magnus,

    I suggest  could use Session or ViewState to store the condition.

    After you change the filter condition, you could change the ViewState value and store the newest filter condition.

    Then, when you bind the ListView, you could check the ViewState and according to the condition to filter the data.

    You could refer to this thread: http://forums.asp.net/p/2083062/6012173.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2016 8:41 AM

All replies

  • User-271186128 posted

    Hi Yeoman,

    However, now I want to also call it within the OnPreRender event of the ListView's DataPager.

    Then, when reaching the instruction myDataSource.SelectParameters.Clear();, I get an exception:

    From your description, I'm confused about that why you want to change the ObjectDataSource Select Method in the OnPreRender event of the ListView's DataPager. 

    The DataPager is in the ListView, when you bind the ListView, it will provide paging functionality according the ObjectDataSource.

    If you want to change the ObjectDataSource select method, I suggest you could use the change event of some controls or the ListView, but not suggest you use the OnPreRender event of the ListView's DataPager.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 21, 2016 5:56 AM
  • User260076833 posted

    Hello Zhi Lv,

    thank you for your advice!

    In my scenario, the set of items the ListView should display, depends on some filter condition. This filter condition may be changed by the user by changing some controls on the form. Then, the contents of the ListView should change immediately.

    However, I found that the ListView "forgets" the current filter condition as soon as the user selects another page with the DataPager. Then, all records are shown and the filter is not applied. I have posted this problem here:

    http://forums.asp.net/t/2083068.aspx?DataPager+causes+ListView+to+show+wrong+data

    After searching the web for a solution, I found that putting the code that updates the ListView into the DataPager's PreRender method solves the problem.
    This is also done in these postings:

    http://stackoverflow.com/questions/3204600/how-do-i-use-a-datapager-with-server-side-paging

    http://www.codedigest.com/Articles/ASPNET/100_Paging_in_ListView_in_ASPNet_35.aspx

    This works for all my ListView's where I use the FilterExpression attribute to filter the list. But in this case, I modify the select statement instead, because the logic is in some stored procedures in the database.

    If you have a better solution, please let me know!

    Otherwise, however, I'd like to know how to reset the parameters of the query without exception...

    Thanks
    Yeoman

    Thursday, January 21, 2016 12:32 PM
  • User-271186128 posted

    Hi Magnus,

    I suggest  could use Session or ViewState to store the condition.

    After you change the filter condition, you could change the ViewState value and store the newest filter condition.

    Then, when you bind the ListView, you could check the ViewState and according to the condition to filter the data.

    You could refer to this thread: http://forums.asp.net/p/2083062/6012173.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2016 8:41 AM