Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type specified in the TypeName property of ObjectDataSource could not be found

Tags:

c#

.net

asp.net

Because of requirements, all website .cs files are stored inside App_Code directory and compiled into App_Code.dll. There is an error when I try to access one specific page of the website.

Description: An unhandled exception occurred during the execution
of the current web request. Please review the stack trace for more
information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The type
specified in the TypeName property of ObjectDataSource
'DataSourceSubmissionList' could not be found.

I have a Gridview control which is populated by ObjectDataSource. Code is below:

/layouts/Portal/Company/Application/code.ascx:

<%@ Control Language="c#" AutoEventWireup="true"
CodeFile="~/layouts/Portal/Company/Application/code.ascx.cs"
Inherits="Project.WebUserControls.myapplications.sublayout" %>

<dx:ASPxGridView ID="ASPxGridView1"
  runat="server"
  DataSourceID="DataSourceSubmissionList"
  KeyFieldName="SubmissionId"
</dx:ASPxGridView>

<asp:ObjectDataSource
  ID="DataSourceSubmissionList"
  runat="server"
  TypeName="Project.WebUserControls.myapplications.sublayout">
</asp:ObjectDataSource>

/layouts/Portal/Company/Application/code.ascx.cs:

namespace Project.WebUserControls.myapplications
{  
    public partial class sublayout: System.Web.UI.UserControl 
    {
    }
}

When I use this line in the code.ascx file to get the fully qualified typename...

<% Response.Write(typeof(Project.WebUserControls.myapplications.sublayout).AssemblyQualifiedName); %>

It prints this on the page.

Project.WebUserControls.myapplications.sublayout, App_Web_oiftguk4,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

However, using this exact type (Project.WebUserControls.myapplications.sublayout) in the ObjectDataSource TypeName results in error.

I have gone through many questions about this error message. I have read that possible solution could be using both the namespace and assembly name in the TypeName property, like this. But I can't do that because code is compiled dynamically and assembly name keeps changing.

Another thing - this error occurs only when CodeFile approach is used. If I switch to CodBehind, no problem.

What could be the reason for this behavior?

like image 228
afaf12 Avatar asked Dec 02 '13 12:12

afaf12


1 Answers

I solved the issue by initializing TypeName property during Page_Init and not specifying TypeName directly in the ObjectDataSource:

/layouts/Portal/Company/Application/code.ascx.cs:

public void Page_Init(object o, EventArgs e)
{
   DataSourceSubmissionList.TypeName = this.GetType().AssemblyQualifiedName;
}

/layouts/Portal/Company/Application/code.ascx:

<asp:ObjectDataSource
  ID="DataSourceSubmissionList"
  runat="server"
</asp:ObjectDataSource>
like image 196
afaf12 Avatar answered Nov 12 '22 10:11

afaf12