I recently had to use the detailsview with my custom object ‘Subscriber’ which had a property ‘Subscription’ of type enum. I ran into a problem when inserting / updating this object. During this operation I received the error ‘Objectdatasource couldnt find a non-generic method….’ .I used the Objectdatasource to bind the ‘Subscriber’ object to the Detailsview. When I did this, the Detailsview didn't not automatically generate the column for the enum property ‘Subcription’, but the ObjectDataSource’s parameters list had this enum listed, I had to manually add this property as a column. After spending sometime I found the solution, which I have presented below.
Enum:
public enum Subscription
{
Unsubscribed,
New,
Subscribed
}
Custom Object:
[DataObject]
public class Subscriber : BusinessObject
{
private int _subscriberID = 0;
public int SubscriberID
{
get { return _subscriberID; }
set { _subscriberID = value; }
}
// I have omitted other properties for this blog
private Subscription _subscription = Subscription.New;
public Subscription Subscription
{
get { return _subscription; }
set { _subscription = value; }
}
Insert/Update Methods for Objectdatasource:
[DataObjectMethod(DataObjectMethodType.Insert, true)]
public static int InsertSubscriber(string firstName, string lastName, Subscription info,....)
[DataObjectMethod(DataObjectMethodType.Update, true)]
public static bool Update(int subscriberID, string firstName, string lastName, Subscription info,....)
Database:
The ‘Subscription’ property is saved as an int in the database.
Detailsview:
The ‘Subscription’ property binds to a dropdownlist for Insert/Edit operations.
DropDownList Properties:
DataSource: Enum.GetNames(typeof(Subscription))
SelectedValue: Bind("Subscription")
Solution:
I had to take the following steps to get it to work.
1. Manually add the column for the enum property, I have called the column ‘Subscription’.
2. Add the following code manually to the Detailsview events Inserting and Updating respectively.
protected void ODSSubscriberDetails_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
DropDownList ddlSubscription = dvSubscriberDetails.FindControl("ddlSubscription") as DropDownList;
e.InputParameters["info"] = ddlSubscription.SelectedValue;
e.InputParameters.Remove("Subscription");
}
protected void ODSSubscriberDetails_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
DropDownList ddlSubscription = dvSubscriberDetails.FindControl("ddlSubscription") as DropDownList;
e.InputParameters["info"] = ddlSubscription.SelectedValue;
e.InputParameters.Remove("Subscription");
}
Alternate Code:
System.Collections.Specialized.IOrderedDictionary parameters = e.InputParameters;
parameters["info"] = ddlSubscription.SelectedValue;
parameters.Remove("Subscription");
In the code above I am assigning the SelectedValue property to the ‘Subscription’ field ‘info’ defined in my Insert/Update methods in my class ‘Subscriber’ used by the ObjectDataSource. I then remove the column ‘Subscription’ I had manually added. The reason for this removal is because the ObjectDataSource will have one parameter more than defined in the Insert/Update methods.
I Hope this helps and saves some hair. I certainly lost some! And a mouse.