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:
- C# Store/Retrieve File in Database Image Field using ODBC
- SQL Create Table Add Description to Column
- Microsoft Word Spell Checker Doesn't Check Cut and Pasted Text
- How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
- Using Nullable Data Types with C#
- ASP.NET Charting Control 3.5 fix for "Error executing child request for ChartImg.axd"
Recent comments
- Never seen this issue
3 days 5 hours ago - Error in query.ToList()
3 days 8 hours ago - Thanks
3 days 21 hours ago - Thanks
6 days 19 hours ago - To get the data working,
1 week 2 days ago - If I manually change the
1 week 2 days ago - Handling EDM Relationship Metadata
1 week 2 days ago - About the itextsharp version
1 week 2 days ago - Not sure
1 week 4 days ago - Green traffic bars
2 weeks 2 days ago
Similar
- Fixing Relative Paths in C# ASP.NET When Using Url Rewriting
- GridView ObjectDataSource LINQ Paging and Sorting
- How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
- Global.asax Events in IIS 6 and IIS 7 for Static Resources
- Create a Windows-style GroupBox in ASP.NET


The list of valid C-Types
The list of valid C-Types defined in the ODBC specification is the same for all ODBC drivers exams preparation. Typically, data store manufacturers create new data types for new scenarios or new customer needs 000-330 exam. Applications usually use the generic C-type SQL_C_BINARY to work with these new data-source specific types 000-331 exam. ODBC 3.80 allows driver manufacturers to define their own C-Types. This means that a driver can define its own client-side type conversion rule for its new driver-specific data type, and thus provide a better developer experience.
iphone blog Saudi Arabia healthy diet