Need a simple CRM and Project Management system?
Check out JobNimbus - CRM for Contractors and Service Professionals.
Check out JobNimbus - CRM for Contractors and Service Professionals.
ObjectDataSource TypeName with Constructor Parameters
Say you have a class for something like a Data Access Layer but the default constructor of the class has a required parameter. For instance, something like this:
public class MyDataClass
{
public MyDataClass(string sConnectionString)
{
// default constructor contains a parameter
// so we need to handle this for the ObjectDataSource.
}
public int SelectCount()
{
// get count for pagination
return 1;
}
public DataTable SelectAll(string sortType, int startRowIndex, int maximumRows)
{
// get data from data source and return as DataTable.
return new DataTable();
}}
By default, the ObjectDataSource requires a parameterless default constructor on the TypeName class to instantiate. To solve this problem, you need to implement the OnObjectCreating event for the ObjectDataSource. Here is an example of how you might set up your ObjectDataSource in the HTML source view:
<asp:ObjectDataSource ID="ods1" runat="server"
EnablePaging="true"
OnObjectCreating="ods1_ObjectCreating"
SelectCountMethod="SelectCount"
SelectMethod="SelectAll"
TypeName="MyDataClass"></asp:ObjectDataSource>
Then implement the OnObjectCreating event to set the TypeName object to an instance using the constructor:
protected void ods1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
string sConnectionString = "Put a connection string here";
MyDataClass myDataClass = new MyDataClass(sConnectionString);
// set ods object
e.ObjectInstance = myDataClass;}
Popular Articles
Last viewed:
- Explanation of Cross Domain and Client Access Policy files for Silverlight
- C# Download File with Progress Bar
- Windows Server 2003 Configure RRAS (Routing and Remote Access Service) Site to Site VPN
- Launch URL in Default Browser using C#
- C# Free Component to Generate PDF - Convert HTML to PDF
- C# Remove or Replace Microsoft Word Special Characters
Recent comments
- Great explanation and more questions
8 hours 3 min ago - Insertion of illegal Element:
4 weeks 3 days ago - Insertion of illegal Element: 32
4 weeks 3 days ago - re "But, this will NOT work."
5 weeks 3 days ago - Unable to cast COM object of t
5 weeks 3 days ago - Saved my life
5 weeks 4 days ago - nice
8 weeks 3 days ago - good article
9 weeks 4 days ago - windows 2008 server backups
11 weeks 3 days ago - code
12 weeks 1 day ago

Thanks
I don't know why gems like these are so hidden on the internet. Thank you so much.
This is exactly what I needed !
Thanks !
Rico