Monset Monset - 18 days ago 11
C# Question

gridview in web forms Could not load type

I have a ASP.NET, WebForms web application and I'm using Entity framework.

I need to put some data from DB to GridView, but when I add the most basic code to my List.aspx page for GridView, I get error saying "Could not Load Type 'Report'" Error:

<asp:GridView ID="gvReports" runat="server" ItemType="Report" SelectMethod="gvReports_GetData"></asp:GridView>


Packages that I'm using:

package id="EntityFramework" version="6.1.3" targetFramework="net452" />
package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />


The model for Entity looks like this:

enter image description here

And the SelectMethod looks like this:

public IQueryable<Report> gvReports_GetData()
{
FinReports_ConceptEntities db = new FinReports_ConceptEntities();
return db.Reports;
}


EDIT I followed this tutorial
https://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data

Answer

This happens because you haven't got the namespace referenced correctly on your page.

There are three solutions:


Use a fully qualified name for your item type.

<asp:GridView ID="gvReports" runat="server" ItemType="MyNamespace.Report" SelectMethod="gvReports_GetData"></asp:GridView>

Import the namespace at the top of your page with an import namespace directive (similar to a using statement in a C# class). See How to add namespace in aspx file?

<%@ Import Namespace="MyNamespace" %>

Or you can import the namespace for all ASPX pages in your project by adding some configuration to web.config file. See How to add namespaces in web.config file?

<configuration>
  <system.web>
    <pages>
      <namespaces>
        <add namespace="MyNamespace" />
      </namespaces>
    </pages>  
  </system.web>
</configuration>

Note that it is not MyProject.MyClass syntax when fully qualifying it. It is MyNamespace.MyClass. It just so happens your project and namespace are the same, but this won't always be the case.

Comments