How to Delete Purchase Orders
For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.
You can delete purchase orders from the orders database in the following ways:
Create an array that contains the OrderGroupId attribute of each PurchaseOrder instance that you want to delete. Then pass the array to the DeletePurchaseOrders method of the PurchaseOrderManager object.
Create a SearchClause object to represent the conditions that a purchase order must match in order to be deleted. Then pass the SearchClause object to the DeletePurchaseOrders method of the PurchaseOrderManager object.
This topic explains how to delete purchase orders by specifying conditions that the purchase orders must match.
To delete purchase orders
Create an OrderManagementContext object.
For more information about creating an OrderManagementContext object, see How to Create an OrderManagementContext Object.
Get the PurchaseOrderManager object from the OrderManagementContext object.
Create a SearchClause object that will match all of the purchase orders that you want to delete.
For more information about creating a SearchClause object, see How to Create a Search Clause.
Call the DeletePurchaseOrders method on the PurchaseOrderManager object and pass the SearchClause object as a parameter.
Example
The following code example deletes purchase orders that have a status of "Completed".
using System;
using System.Data;
using System.Globalization;
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Orders;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
// Create the OrderManagementContext object. This
// example accesses the Orders System in local mode by
// creating an OrderSiteAgent object. You could also
// access the Orders System in agent mode by creating
// an OrderServiceAgent object.
// In the following, replace "StarterSite" with the
// name of your site.
OrderSiteAgent ordersAgent = new OrderSiteAgent("StarterSite");
OrderManagementContext context = OrderManagementContext.Create(ordersAgent);
PurchaseOrderManager manager = context.PurchaseOrderManager;
// Create a search clause.
DataSet searchableProperties = manager.GetSearchableProperties(CultureInfo.CurrentUICulture.ToString());
SearchClauseFactory searchClauseFactory = manager.GetSearchClauseFactory(searchableProperties, "PurchaseOrder");
SearchClause clause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Status", "Completed");
// Delete purchase orders that match the conditions.
int recordsDeleted;
manager.DeletePurchaseOrders(clause, out recordsDeleted);
Console.WriteLine("Deleted " + recordsDeleted.ToString() + " records.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message);
if (ex.InnerException != null)
{
Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
}
Console.ReadLine();
}
}
}
}
Your database must contain purchase orders whose Status field contains the string "Completed" for this example to delete any records.
Compiling the Code
To run this code example, create a console application and add references to the following assemblies:
Microsoft.CommerceServer.CrossTierTypes.dll
Microsoft.CommerceServer.Orders.CrossTierTypes.dll
Microsoft.CommerceServer.Orders.DataManagement.dll