ASP.NET: Dealing with object types as parameters when updating in ObjectDataSource
-
23-08-2019 - |
Question
How do you guys update let's say for example a FormView with an ObjectDataSource source. The DataObjectTypeName is a class which I have already marked with a DataObject attribute. Now, I want to customize the update process and add some custom data to the parameter. What do we need to do so?
Example: I have a BLL class which let's call it "ProductsBLL" and a data class "Product".
I declare the following ObjectDataSource control:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="Product" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetProduct" TypeName="Assembly.ProductsBLL"
UpdateMethod="UpdateProduct">
<UpdateParameters>
<asp:Parameter Name="product" Type="Object" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="productID" QueryStringField="ProdID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Given that the Update method in the ProductsBLL class accepts a Product object as a parameter. Now, before the update takes place, I want to add a custom data to the Product parameter. How can I do that?
Solution
I'm not sure exactly what your question is (you need to be more specific), but the general flow is as follows:
Your GridView is linked to an ObjectDataSource.
Your ObjectDataSource is linked to a Biz Logic layer via the
TypeName
property - This is the class that will be instantiated for performing data operations. (call itProductsBLL
for example).It also uses an
DataObjectTypeName
property which is the type of object being retrieved/updated/deleted by the BLL. (say,Product
)Also, it specifies the methods in the BLL to call whenever an action is invoked (
UpdateMethod
,SelectMethod
, etc.) (say,ProductsBLL.UpdateProducts
,ProductsBLL.DeleteProducts
)Your BLL object then performs custom operations such as validation logic on the data received from the ObjectDataSource and calls your Datalayer to perform the actual updation/deletion from the database. (say,
ProductsDataLayer.UpdateProduct()
)
If you need more information, please edit your question to be more specific.
Edit (after edit to original question):
Modifying the Product
parameter before update would be as simple as:
// In Biz Logic Layer.
public int UpdateProduct(Product p)
{
// Modify the ProductName and the Price properties of this Product.
p.ProductName = "Product " + p.ProductName;
p.Price = 0.95 * p.Price;
// Call DataLayer.
return ProductDL.UpdateProduct(p.ID, p.ProductName, p.Quantity, p.Price);
}
where the ProductDL.UpdateProduct
might look like:
public int UpdateProduct
(
string productID,
string productName,
int productQty,
float productPrice
)
And, so on.
OTHER TIPS
In the ObjectDataSource updating event, check the e.InputParameters collection, your Products object should be stored in e.InputParameters[0].