Binding with Entity Framework

5 posts, 1 answers
  1. Dave
    Dave avatar
    29 posts
    Member since:
    Aug 2010

    Posted 28 Jan 2014 Link to this post

    I'm trying to follow the demo shown here but when I run it as is I receive the following:

    [NotSupportedException: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().]
       System.Data.Entity.Infrastructure.DbQuery`1.System.ComponentModel.IListSource.GetList() +44
       System.Web.UI.DataSourceHelper.GetResolvedDataSource(Object dataSource, String dataMember) +57
       System.Web.UI.WebControls.ReadOnlyDataSource.System.Web.UI.IDataSource.GetView(String viewName) +42
       System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +145
       System.Web.UI.WebControls.DataBoundControl.GetData() +4
       Telerik.Web.UI.RadGrid.GetData() +132
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +54
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
       Telerik.Web.UI.RadGrid.DataBind() +107
       Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +4252
       Telerik.Web.UI.RadGrid.OnLoad(EventArgs e) +201
       System.Web.UI.Control.LoadRecursive() +54
       System.Web.UI.Control.LoadRecursive() +145
       System.Web.UI.Control.LoadRecursive() +145
       System.Web.UI.Control.LoadRecursive() +145
       System.Web.UI.Control.LoadRecursive() +145
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

    However, if I just select a subset and call ToList() on it, it doesn't throw the error.

    Is there a new demo that shows the latest way of binding with EF? I will be using all the paging, filtering, sorting and grouping features.

    Regards.
  2. Dave
    Dave avatar
    29 posts
    Member since:
    Aug 2010

    Posted 28 Jan 2014 Link to this post

    I've changed the grid to instead use an EntityDataSource and things seem to be working alright now. The paging, sorting, filtering work quite impressively.

    But now I'm running into a new issue. I would like to be able to have the user make changes, then when they save these changes I will manually do the updating through a separate connection that contains all of the logic.

    Here is how I currently have things setup:
    <telerik:RadGrid
        ID="RadGridPrototype"
        runat="server"
        AutoGenerateColumns="false"
        AllowSorting="True"
        AllowPaging="True"
        AllowFilteringByColumn="True"
        MasterTableView-AllowMultiColumnSorting="true"
        GridLines="none"
        PageSize="25"
        ShowGroupPanel="true"
        EnableHeaderContextMenu="true"
        EnableHeaderContextFilterMenu="true"
        DataSourceID="EntityDataSource1"
        OnItemDataBound="RadGridPrototype_ItemDataBound"
        OnUpdateCommand="RadGridPrototype_UpdateCommand">
        <MasterTableView DataKeyNames="ID" CommandItemDisplay="Bottom" EditMode="Batch">
            <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="true" />
            <BatchEditingSettings EditType="Cell" OpenEditingEvent="DblClick" />
            <PagerStyle Mode="NextPrevAndNumeric" PagerTextFormat="{4} Page {0} of {1} | Displaying rows {2}-{3} of {5}" />
        </MasterTableView>
        <ClientSettings AllowDragToGroup="true">
        </ClientSettings>
    </telerik:RadGrid>
     
    <asp:EntityDataSource
        ID="EntityDataSource1"
        runat="server"
        ConnectionString="name=TicketContext"
        DefaultContainerName="TicketContext"
        EntitySetName="Ticket_NonFiltered"
        OnUpdating="EntityDataSourceTickets_Updating" />

    Then in the code behind at the RadGridPrototype_UpdateCommand function:

    protected void RadGridPrototype_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        var editedItem = ((GridEditableItem)e.Item);
     
        string rowID = (string)editedItem.GetDataKeyValue("ID");
     
        //Option 1
        using (DBItemContext context = new DBItemContext())
        {
            var rowValues = context.NeededTable.Where(n => n.Id == rowID).FirstOrDefault();
            if (rowValues != null)
            {
                // You can actually make changes here to the entity:
                rowValues.Field1 = "garbage"; // this will be returned to original value
     
                // This call restores all the original values, none of the edited values from the grid come in.
                editedItem.UpdateValues(rowValues);
            }
        }
     
        //Option 2
        Hashtable newValues = new Hashtable();
        editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
        // The newValues hash has all the row values, but none of them are the edited ones from the grid.
    }


    The page posts back before calling the UpdateCommand method, so the values I retrieve aren't actually the changed ones and both of the methods above of trying to get the edited values just return the originals.

    What should my approach here be? I'm assuming going back to having the advanced binding could possibly fix it, but when using the NeedDataSource event since you have to call ToList() to set the source there's no automatic paging and the entire set was being retrieved each time which takes much too long.

     

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Jan 2014 in reply to Dave Link to this post

    Hi Dave,

    You can handle the Batch edit operations in the OnBatchEditCommand event of the RadGrid. Please try the following code snippet:

    C#:
    protected void RadGridPrototype_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
    {
     foreach (GridBatchEditingCommand command in e.Commands)
      {
       if ((command.Type == GridBatchEditingCommandType.Update)) // For Update
        {
          Hashtable newValues = command.NewValues;            
          string ID= newValues["ID"].ToString();              
        }
      }
    }

    Thanks,
    Shinu
  5. Dave
    Dave avatar
    29 posts
    Member since:
    Aug 2010

    Posted 29 Jan 2014 in reply to Shinu Link to this post

    Hi Shinu,

    That seems to work great, thank you!

    I also noticed that the same thing can be handled in the ItemCommand event:

    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            Hashtable updatedRowValues = (e.CommandArgument as GridBatchEditingEventArgument).NewValues;
            string ID = updatedRowValues["ID"].ToString();
        }
    }

    Would there be a reason to prefer one over the other?

    Thank you.
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Jan 2014 in reply to Dave Link to this post

    Hi Dave,

    Please go through the Server-side API topic in this documentation on Batch Editing. It discuss the use of different events.

    Thanks,
    Shinu
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017