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! --

9 Comments

  • 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.

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

  • 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 http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=368 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?

  • 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?

  • thanks! this is really helpful

  • I believe this site has very excellent composed subject material blog posts.

  • Appreciate my best brand-new Buy Mulberry!! Have not undertaken him or her right off they may be as a result fashion!! Are interested in the whole set of Buy Mulberry hahah xxx

  • swtor credits excellente qualité des rubrique excellente adresse.. RA-VIE!!

  • hello amazing article

Comments have been disabled for this content.