Wednesday, October 10, 2007 10:50 PM srkirkland

Using BLL objects with Parameterized Constructors in an ObjectDataSource

If you use BLL objects with ObjectDataSources to populate your GridViews, DropDownLists, etc, the ObjectDataSource tries to instantiate your BLL object using a parameterless constructor. If you don't have a parameterless constructor (I usually don't) available, you'll get and error like:

No parameterless constructor defined for this object.

Description: An un-handled 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.MissingMethodException: No parameterless constructor defined for this object.

The way to get around this problem is to handle the ObjectDataSource's "OnObjectCreating" property and instantiate the object with any necessary parameters, and then assign that object to the e.ObjectInstance property. A quick and easy sample is below:

protected void objBLLClass_ObjectCreating(object sender, ObjectDataSourceEventArgs e)

{

BLLClass bClass = new BLLClass(param1, param2);

e.ObjectInstance = bClass;

}

-- If you aren't using ObjectsDataSources yet, I highly recommend it for creating powerful, multi-tiered applications. There is a great intro-level series on 3-Tier Data Access at the ASP.NET main site. Once you understand the .NET 2.0 data model, there are many ways to customize the default objects to create really powerful applications! --

Filed under: ,

Comments

# links for 2007-10-12 « ThisIsSteve

Friday, October 12, 2007 9:21 AM by links for 2007-10-12 « ThisIsSteve

Pingback from  links for 2007-10-12 « ThisIsSteve

# re: Using BLL objects with Parameterized Constructors in an ObjectDataSource

Tuesday, March 04, 2008 4:04 PM by Andy

Hi Scott,

I have the same problem. I tried the above, without success. Any other thoughts? I noticed in the asp.net objectdatasource tutorials that they don't even bother with a constructor in the BLL.

# re: Using BLL objects with Parameterized Constructors in an ObjectDataSource

Tuesday, April 08, 2008 11:59 AM by MrDynamic

You sir, ROCK!  worked great thanks for posting !!

# re: Using BLL objects with Parameterized Constructors in an ObjectDataSource

Monday, May 19, 2008 4:58 PM by vkelman

Hi Scott! I'm using static myBLLClass.GetItem() and  myBLLClass.GetList() methods which return correspondingly new BusinessObject and new list of BusinessObjects.

(There are static Insert()/Update() methods as well.)

I don't have parameterless constructor (I use one with parameters, so there is no default constructor generated.)

It does not raise an error when I *display* an object or a list, but it does raise an error complaining on the absence of parameterless constructor when I try to *save* updated data. On imar.spaanjaars.com/QuickDocId.aspx author says that having static methods solves "No parameterless constructor defined" problem. It does, but only when information is being read.

Any thoughts on how to allow to update information without adding parametrless constructor? Should I use another event?

# re: Using BLL objects with Parameterized Constructors in an ObjectDataSource

Wednesday, September 24, 2008 3:51 PM by benyu123

I have exactly the same problem as vkelman encountered. My BLL class has a default parameterless constructor, but the compiler complained when i am doing the update method. Read method does not complained about this error message. Could anybody know why?

Leave a Comment

(required) 
(required) 
(optional)
(required)