The type specified in the TypeName property of ObjectDataSource was not found

Due to requirements, all .cs website files are stored in the App_Code directory and compiled into App_Code.dll. When you try to access one specific page of a website, an error occurs.

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 that is populated with an ObjectDataSource. Code 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 full type name ...

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

He prints it 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 ObjectNataSource TypeName results in an error.

I had a lot of questions about this error message. I read that as a possible solution, you can use the namespace name and assembly in the TypeName property like this . But I cannot do this because the code is compiled dynamically and the assembly name continues to change.

Another thing is that this error only occurs when using the CodeFile approach. If I switch to CodBehind, no problem.

What could be causing this behavior?

+6
source share
1 answer

I solved the problem by initializing the 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> 
+17
source

Source: https://habr.com/ru/post/959170/


All Articles