Sample: Deleting event handler method
The following C# example shows the Deleting event handler method used to delete contact history information for a customer object. The event handler examines the customer object to retrieve the customer ID value. After retrieving a connection to the current company's database and storing it in the connection private variable, the event handler executes a SQL statement to delete the contact history information for the customer from the IG003 table.
// Declare private variable of type Microsoft.Dynamics.Common.Connection private static Connection connection; public static void DeleteContactHistory(object sender, BusinessObjectEventArgs e) { string deleteStatement; Customer customer; if (e.BusinessObject.GetType() == typeof(Customer)) { customer = (Customer)e.BusinessObject; // Get the connection to the database for the current company connection = Connection.GetInstance(); // The SQL statement to delete contact history information deleteStatement = "DELETE FROM IG003 WHERE CUSTNMBR = '" + customer.Key.Id + "'"; // Create the SQL connection SqlCommand command = new SqlCommand(deleteStatement); SqlConnection sqlConnection = new SqlConnection (connection.GetConnectionString(e.Context.OrganizationKey)); command.Connection = sqlConnection; // Open the SQL connection sqlConnection.Open(); // Execute the SQL statement command.ExecuteNonQuery(); // Close the SQL connection sqlConnection.Close(); } }