0

Goal: Display data from SQL View into a ListView on an asp.net page There is a new database with a new view and I need to point it to that

ERROR:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' Inner Exception NotSupportedException: The specified type member 'Alternate_ID' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

System.InvalidOperationException: 'When the DataBoundControl has paging enabled, either the SelectMethod should return an IQueryable or should have all these mandatory parameters : int startRowIndex, int maximumRows, out int totalRowCount'

Tried: I've looked through the tutorials on listview, documentation, itemtype, data binding, etc. One of the SO I found was : The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported Ive looked into the models, views, checked spelling, designer files, pocos, etc.

CODE:

default.aspx:

 <asp:ListView id="lstClaims" runat="server" ItemType="Placeholder.Data.vx_EligibilitySearch" SelectMethod="lstMembers_GetData" OnItemCommand="lstMembers_ItemCommand" 
        ItemPlaceholderID="litPlaceHolder">
 <LayoutTemplate>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th></th>
                            <th><asp:LinkButton id="Sort_Claim_Number" runat="server" CommandArgument="Group_Description" CommandName="Sort">Group</asp:LinkButton></th>
                            <th><asp:LinkButton id="Sort_Claim_Status" runat="server" CommandArgument="Group_ID" CommandName="Sort">Group ID</asp:LinkButton></th>
                            <th><asp:LinkButton id="Sort_Plan" runat="server" CommandArgument="Alternate_ID" CommandName="Sort">Member ID</asp:LinkButton></th>

</thead>
                    <asp:Literal ID="litPlaceHolder" runat="server" />
                </table>
            </div>
        </LayoutTemplate>



 <ItemTemplate>
            <tr>
                <td><%# Item.Group_Description %></td>
                <td><%# Item.Group_ID %></td>
                <td><%# Item.Alternate_ID %></td>

  </tr>
        </ItemTemplate>
    </asp:ListView>

Note: protected global::System.Web.UI.WebControls.ListView lstClaims; is in default.aspx.designer.cs

This points to [ lstClaims.DataBind(); ] :

  if (memberIDIsValid || memberIsValid)
            {
                ShouldSearch = true;
                lstClaims.DataBind();
            }

This is in the method: lnkSearch_Click, which is called from the aspx page here (When clicking the Search button):

<p><asp:LinkButton ID="lnkSearch" runat="server" OnClick="lnkSearch_Click" CssClass="btn btn-default"><i class="fa fa-search"></i> Search Members</asp:LinkButton></p>

Method to get Data:

public IQueryable<vx_EligibilitySearch> lstMembers_GetData()
        {
            litNumSearchResults.Text = "";

            // IQueryable<Member> members = null;
            IQueryable<vx_EligibilitySearch> members = null;


            try
            {
                if (!ShouldSearch)
                    return members;

                panelSearchForm.Visible = false;
                lnkButtonBackToSearch.Visible = true;

                members = GetDataFromQuery();


public IQueryable<vx_EligibilitySearch> GetDataFromQuery()  
        {
            // IQueryable<Member> members = null;
            IQueryable<vx_EligibilitySearch> members = null;



            try
            {
                // members = somedb.Members.AsNoTracking().AsQueryable(); //.OrderBy(a => a.Claim_Number)
                members = somedb.vx_EligibilitySearch.AsNoTracking().AsQueryable(); //.OrderBy(a => a.Claim_Number)


                if (!string.IsNullOrEmpty(Search_MemberID))
                    // members = members.Where(m => m.Subscriber_ID == Search_MemberID).AsQueryable();
                    members = members.Where(m => m.Alternate_ID == Search_MemberID).AsQueryable();

In partial class vx_EligibilitySearch I have:

public string Alternate_ID { get; set; }

More:

namespace Placeholder.Data
{
    using System;
    using System.Collections.Generic;

    public partial class vx_EligibilitySearch
    {
        public string Subscriber_ID { get; set; }


        public string Group_Description { get; set; }
        public string Group_ID { get; set; }
        public string Alternate_ID { get; set; }
        public string Member_Name { get; set; }
        public string Sex { get; set; }
        public Nullable<System.DateTime> Birth_Date { get; set; }

DbContext :

members = somedb.vx_EligibilitySearch.AsNoTracking().AsQueryable(); //.OrderBy(a => a.Claim_Number)

Going to Definition of somedb takes me to BasePage.cs with:

protected VBADemoEntities somedb = new VBADemoEntities();

When I go to definition of VBADemoEntities:

namespace Placeholder.Data

public partial class VBADemoEntities : DbContext
    {
        public VBADemoEntities()
            : base("name=VBADemoEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Auth> Auths { get; set; }
        ...........
        public virtual DbSet<Member> Members { get; set; }
        public virtual DbSet<vx_EligibilitySearch> vx_EligibilitySearch { get; set; }
seesharp
  • 101
  • 1
  • 14
  • `somedb.vx_EligibilitySearch` what is `vx_EligibilitySearch` mapping to in the backend database? Is it some view? – Mohsin Mehmood Aug 22 '18 at 17:21
  • Yes there is a View in SQL Server with that name – seesharp Aug 22 '18 at 17:25
  • Are you using Entity Framework core? Ensure your view returns Alternate_ID column. Share code related to how SQL View is mapped to `vx_EligibilitySearch` class? – Mohsin Mehmood Aug 22 '18 at 17:29
  • I have EntityFramework in the References. I added some more of the vx_EligibilitySearch.cs at the bottom. Is this what you were looking for? – seesharp Aug 22 '18 at 17:56
  • Does `vx_EligibilitySearch` returns `Alternate_ID` column when you select from this view? Also, can you share your DbContext class `somedb` code? – Mohsin Mehmood Aug 22 '18 at 18:03
  • When I run that View in SQL Server it returns data. I just added DbContext to the post – seesharp Aug 22 '18 at 18:12

1 Answers1

0

You cannot do object comparison inside the Entity Framework query because EF need to convert your LINQ statements to SQL Queries in order to perform any actions(query).

Agnel Amodia
  • 765
  • 8
  • 18
  • I'm not sure I understand your comment. How would you not be able to read from a View? Why would they exist if they cant be used. Isn't this saying the opposite ? https://www.mssqltips.com/sqlservertip/1990/how-to-use-sql-server-views-with-the-entity-framework/ – seesharp Aug 22 '18 at 20:18