/// <summary>
        /// Saves this ProductImage object to the database.
        /// </summary>
        /// <returns><b>SaveResult</b> enumeration that represents the result of the save operation.</returns>
        public virtual SaveResult Save()
        {
            if (this.IsDirty)
            {
                Database database     = Token.Instance.Database;
                bool     recordExists = true;

                if (this.ProductImageId == 0)
                {
                    recordExists = false;
                }

                if (this.OrderBy < 0)
                {
                    this.OrderBy = ProductImageDataSource.GetNextOrderBy(this.ProductId);
                }

                if (recordExists)
                {
                    //verify whether record is already present
                    StringBuilder selectQuery = new StringBuilder();
                    selectQuery.Append("SELECT COUNT(*) As RecordCount FROM ac_ProductImages");
                    selectQuery.Append(" WHERE ProductImageId = @ProductImageId");
                    using (DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()))
                    {
                        database.AddInParameter(selectCommand, "@ProductImageId", System.Data.DbType.Int32, this.ProductImageId);
                        if ((int)database.ExecuteScalar(selectCommand) == 0)
                        {
                            recordExists = false;
                        }
                    }
                }

                int result = 0;
                if (recordExists)
                {
                    //UPDATE
                    StringBuilder updateQuery = new StringBuilder();
                    updateQuery.Append("UPDATE ac_ProductImages SET ");
                    updateQuery.Append("ProductId = @ProductId");
                    updateQuery.Append(", ImageUrl = @ImageUrl");
                    updateQuery.Append(", ImageAltText = @ImageAltText");
                    updateQuery.Append(", OrderBy = @OrderBy");
                    updateQuery.Append(" WHERE ProductImageId = @ProductImageId");
                    using (DbCommand updateCommand = database.GetSqlStringCommand(updateQuery.ToString()))
                    {
                        database.AddInParameter(updateCommand, "@ProductImageId", System.Data.DbType.Int32, this.ProductImageId);
                        database.AddInParameter(updateCommand, "@ProductId", System.Data.DbType.Int32, this.ProductId);
                        database.AddInParameter(updateCommand, "@ImageUrl", System.Data.DbType.String, this.ImageUrl);
                        database.AddInParameter(updateCommand, "@ImageAltText", System.Data.DbType.String, NullableData.DbNullify(this.ImageAltText));
                        database.AddInParameter(updateCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy);
                        //RESULT IS NUMBER OF RECORDS AFFECTED
                        result = database.ExecuteNonQuery(updateCommand);
                    }
                }
                else
                {
                    //INSERT
                    StringBuilder insertQuery = new StringBuilder();
                    insertQuery.Append("INSERT INTO ac_ProductImages (ProductId, ImageUrl, ImageAltText, OrderBy)");
                    insertQuery.Append(" VALUES (@ProductId, @ImageUrl, @ImageAltText, @OrderBy)");
                    insertQuery.Append("; SELECT Scope_Identity()");
                    using (DbCommand insertCommand = database.GetSqlStringCommand(insertQuery.ToString()))
                    {
                        database.AddInParameter(insertCommand, "@ProductImageId", System.Data.DbType.Int32, this.ProductImageId);
                        database.AddInParameter(insertCommand, "@ProductId", System.Data.DbType.Int32, this.ProductId);
                        database.AddInParameter(insertCommand, "@ImageUrl", System.Data.DbType.String, this.ImageUrl);
                        database.AddInParameter(insertCommand, "@ImageAltText", System.Data.DbType.String, NullableData.DbNullify(this.ImageAltText));
                        database.AddInParameter(insertCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy);
                        //RESULT IS NEW IDENTITY;
                        result = AlwaysConvert.ToInt(database.ExecuteScalar(insertCommand));
                        this._ProductImageId = result;
                    }
                }

                //OBJECT IS DIRTY IF NO RECORDS WERE UPDATED OR INSERTED
                this.IsDirty = (result == 0);
                if (this.IsDirty)
                {
                    return(SaveResult.Failed);
                }
                else
                {
                    return(recordExists ? SaveResult.RecordUpdated : SaveResult.RecordInserted);
                }
            }

            //SAVE IS SUCCESSFUL IF OBJECT IS NOT DIRTY
            return(SaveResult.NotDirty);
        }
 public static ProductImage Load(Int32 productImageId)
 {
     return(ProductImageDataSource.Load(productImageId, true));
 }