Tuesday, 9 June 2009

ObjectDataSource: change insert params in codebehind

I have had mixed feelings about the objectdatasource since it popped up with .net 2.0, true it is disconcertingly easy to put in simple CRUD operations, and allows you to use generics/objects using reflection without having to muck around with datasets,

but

the process has seemed completely 'sealed' beacuase the ODS is reflected using statics to in page objects, if you try and manipulate an objects properties from code behind you hit problems immediately and yet ODS is 95% of a good solution, but adding custom JIT values to an insert was lloking like quite a lot of code.

I have found you can access the object in a create in code-behind without having to recreate the object by instancing the inner object in the dictionary on inserting.

so this in the codebehind

C#

protected void Review_ItemInserting(Object sender, ObjectDataSourceMethodEventArgs e)
{


foreach (DictionaryEntry keyEntry in e.InputParameters)
{

Review review = (Review)keyEntry.Value;
review.EmployeeID = this.Employeeid.Value;
review.LocationID = parseDD(this.gvLocations.SelectedDataKey.Value.ToString());
review.FirstName = this.FirstName.Value;
review.Surname = this.Surname.Value;

}
}



this in the front

<asp:ObjectDataSource ID="Review" runat="server" TypeName="LocationRelay" DataObjectTypeName="Review" DeleteMethod="DeleteReview" InsertMethod="CreateReview" SelectMethod="GetReviewByID" UpdateMethod="UpdateReview" OnInserting="Review_ItemInserting">
<SelectParameters>
<asp:ControlParameter ControlID="DetailTabContainer$ReviewsTabPanel$ReviewsGridView" DefaultValue="0" Name="LocationReviewID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="LocationID" Type="Int32" DefaultValue="0"/>
<asp:Parameter Name="EmployeeID" Type="Int32" DefaultValue="0"/>
<asp:Parameter DefaultValue="0" Name="FirstName" Type="String" />
<asp:Parameter DefaultValue="0" Name="Surname" Type="String" />
</InsertParameters>

</asp:ObjectDataSource>


and this in your bll


[DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
public static Int32 CreateReview(Review review)
{
Int32 intout;
intout = LocationData.CreateReview(review);
return intout;
}

don't know if this has been posted by anyone else, but i haven't found it yet, everyone is probably using linq now anyways.

No comments:

Post a Comment