Configuring the ObjectDataSource for Sorting

2k Views Asked by At
System.NotSupportedException was unhandled by user code
  Message=The data source does not support sorting.
  Source=namespace
  StackTrace:
       at namespace.Admin.ToolkitScriptManager1_AsyncPostBackError(Object sender, AsyncPostBackErrorEventArgs e) in C:\project\Master.cs:line 27
       at System.Web.UI.ScriptManager.OnAsyncPostBackError(AsyncPostBackErrorEventArgs e)
       at System.Web.UI.PageRequestManager.OnPageError(Object sender, EventArgs e)
       at System.Web.UI.TemplateControl.OnError(EventArgs e)
       at System.Web.UI.Page.HandleError(Exception e)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:



  protected void ToolkitScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            ToolkitScriptManager1.AsyncPostBackErrorMessage = Resources.Strings.ErrorAsyncPostBack;
            logger.Error("An asyncpostbackerror has occurred: " + e.Exception.Message);
            throw e.Exception;  // <<<<<< throw error here
        }


  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
         DataObjectTypeName="mynamespace.Product" SelectMethod="GetAllProducts" SortParameterName="sortedBy" 
          TypeName="mynamespace.Products"  UpdateMethod="Update">              
           <SelectParameters>
                <asp:Parameter Name="sortedBy" DefaultValue="DisplayOrdinal, Name" /> 
            </SelectParameters>   
           </asp:ObjectDataSource>

below is my code using for simple gridview and i am only having problem with the sorting and paging is working as expected and i even define the ONSorting event but it still throws me an error: The data source does not support sorting.

namespace mynamespace
{
public class Product
{
private int _id;

public int Id
{
get { return _id; }
set { _id = value; }
}
private string _code;

public string Code
{
get { return _code; }
set { _code = value; }
}
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}


<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id" 
AutoGenerateColumns="False" OnSorting="gridview1_Sorting" DataSourceID="ObjectDataSource1" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" 
SortExpression="Id" ReadOnly="True" />
<asp:BoundField DataField="Code" HeaderText="Code" 
SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" 
SortExpression="Name" />
<asp:TemplateField HeaderText="State/Province" SortExpression="StateProvinceName"
                    HeaderStyle-Wrap="true" HeaderStyle-Width="80px">
                    <ItemTemplate>
                        <%# ((namespace)(Container.DataItem)).StateProvince.Name.ToString()%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlState" runat="server" DataSourceID="StateDataSource" DataTextField="Name"
                            DataValueField="StateProvinceId" AutoPostBack="false">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>

</Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
DataObjectTypeName="mynamespace.Product"
SelectMethod="GetAllProducts" TypeName="mynamespace.Products" 
UpdateMethod="Update">
</asp:ObjectDataSource>




protected void gridview1_Sorting(object sender, GridViewSortEventArgs e)
        { 
            if (e.SortExpression == SortField)
            {
                if (SortDirection != null)
                {
                    if (SortDirection == "ASC")
                        SortDirection = "DESC";
                    else
                        SortDirection = "ASC";
                }
                else
                {
                    if (e.SortDirection == System.Web.UI.WebControls.SortDirection.Ascending)
                        SortDirection = "ASC";
                    else
                        SortDirection = "DESC";
                }
            }
            else
            {
                if (e.SortDirection == System.Web.UI.WebControls.SortDirection.Ascending)
                    SortDirection = "ASC";
                else
                    SortDirection = "DESC";
            }
            SortField = e.SortExpression; 
            DataBind(); 
        }


  protected override void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSourceID = "ObjectDataSource1";

        }


 protected void somecustomlinks_ItemCommand(object source, EventArgs e)
        {
            DataBind();
        }

public override void DataBind()
{
  List<Organization> _org = new List<Organization>();
  Gridview1.DataSourceID = string.Empty;
  Gridview1.DataSource = _orgFilter;
  Gridview1.DataBind();

}
1

There are 1 best solutions below

3
On BEST ANSWER

In order to support sorting with an ObjectDataSource, the Select method of your business object (GetAllProducts() in your case) should take an parameter containing the sort expression to apply.

You also have to set the SortParameterName property of your ObjectDataSource to the name of that parameter.