private void Child_Insert(Category category, SqlConnection connection) { bool cancel = false; OnChildInserting(category, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); using(var command = new SqlCommand("[dbo].[CSLA_Product_Insert]", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@p_ProductId", this.ProductId); command.Parameters.AddWithValue("@p_CategoryId", category != null ? category.CategoryId : this.CategoryId); command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name)); command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(this.Description)); command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(this.Image)); command.ExecuteNonQuery(); // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query. if(category != null && category.CategoryId != this.CategoryId) LoadProperty(_categoryIdProperty, category.CategoryId); // Update the original non-identity primary key value. LoadProperty(_originalProductIdProperty, this.ProductId); } // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). // TODO: Please override OnChildInserted() and insert this child manually. // FieldManager.UpdateChildren(this, null, connection); OnChildInserted(); }
private void Child_Update(Category category, SqlConnection connection) { bool cancel = false; OnChildUpdating(category, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); using(var command = new SqlCommand("[dbo].[CSLA_Product_Update]", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@p_OriginalProductId", this.OriginalProductId); command.Parameters.AddWithValue("@p_ProductId", this.ProductId); command.Parameters.AddWithValue("@p_CategoryId", category != null ? category.CategoryId : this.CategoryId); command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name)); command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(this.Description)); command.Parameters.AddWithValue("@p_Image", ADOHelper.NullCheck(this.Image)); //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); // Update non-identity primary key value. LoadProperty(_productIdProperty,(System.String)command.Parameters["@p_ProductId"].Value); // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query. if(category != null && category.CategoryId != this.CategoryId) LoadProperty(_categoryIdProperty, category.CategoryId); // Update non-identity primary key value. LoadProperty(_originalProductIdProperty, this.ProductId); } FieldManager.UpdateChildren(this, null, connection); OnChildUpdated(); }
protected override void DataPortal_Update() { bool cancel = false; OnUpdating(ref cancel); if (cancel) return; if(OriginalCategoryId != CategoryId) { // Insert new child. Category item = new Category {CategoryId = CategoryId, Name = Name, Description = Description}; item = item.Save(); // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships. foreach(Product itemToUpdate in Products) { itemToUpdate.CategoryId = CategoryId; } // Create a new connection. using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); FieldManager.UpdateChildren(this, connection); } // Delete the old. var criteria = new CategoryCriteria {CategoryId = OriginalCategoryId}; DataPortal_Delete(criteria); // Mark the original as the new one. OriginalCategoryId = CategoryId; OnUpdated(); return; } using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using(var command = new SqlCommand("[dbo].[CSLA_Category_Update]", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@p_OriginalCategoryId", this.OriginalCategoryId); command.Parameters.AddWithValue("@p_CategoryId", this.CategoryId); command.Parameters.AddWithValue("@p_Name", ADOHelper.NullCheck(this.Name)); command.Parameters.AddWithValue("@p_Descn", ADOHelper.NullCheck(this.Description)); //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); LoadProperty(_originalCategoryIdProperty, this.CategoryId); } FieldManager.UpdateChildren(this, connection); } OnUpdated(); }