Method to create new EPiServer Commerce Catalog
Here is my method to create working catalog
public static bool Create(string catalogName) { try { CatalogDto catalogDto1 = CatalogContext.Current.GetCatalogDto(-1, new CatalogResponseGroup(CatalogResponseGroup.ResponseGroup.CatalogInfo)); CatalogDto.CatalogRow newCatalogRow = catalogDto1.Catalog.NewCatalogRow(); newCatalogRow.Name = catalogName; newCatalogRow.StartDate = DateTime.Now; newCatalogRow.EndDate = DateTime.Now.AddYears(20); newCatalogRow.IsActive = true; newCatalogRow.SortOrder = 0; newCatalogRow.ApplicationId = AppContext.Current.ApplicationId; newCatalogRow.Created = DateTime.Now; newCatalogRow.Modified = DateTime.Now; newCatalogRow.IsPrimary = false; newCatalogRow.DefaultLanguage = "sv-SE"; // save catalogDto1.Catalog.AddCatalogRow(newCatalogRow); CatalogContext.Current.SaveCatalog(catalogDto1); return true; } catch { return false; } }
Method to delete an EPiServer Commerce Catalog
public static void Delete(string catalogName) { CatalogDto catalogs = CatalogContext.Current.GetCatalogDto(); CatalogDto.CatalogRow catalog = catalogs.Catalog.FirstOrDefault(x => x.Name.ToLower().Equals(catalogName.ToLower())); CatalogContext.Current.DeleteCatalog(catalog.CatalogId); }
The full error
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
Stack Trace:
[InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.] Mediachase.Commerce.Catalog.Dto.CatalogRow.get_DefaultLanguage() +80 [StrongTypingException: The value for column 'DefaultLanguage' in table 'Catalog' is DBNull.] Mediachase.Commerce.Catalog.Dto.CatalogRow.get_DefaultLanguage() +193 EPiServer.Business.Commerce.HttpModules.<>c__DisplayClass1b.<GetAllCommerceLanguages>b__18(CatalogRow catalogRow) +29 System.Collections.Generic.List`1.ForEach(Action`1 action) +76 EPiServer.Business.Commerce.HttpModules.ProductUrlRewriteModule.GetAllCommerceLanguages() +337 EPiServer.Business.Commerce.HttpModules.ProductUrlRewriteModule.GetProductByLink(UrlBuilder url) +729 EPiServer.Business.Commerce.HttpModules.ProductUrlRewriteModule.UrlRewriteProvider_ConvertingToInternal(Object sender, UrlRewriteEventArgs e) +147 EPiServer.Web.UrlRewriteProvider.OnConvertingToInternal(UrlRewriteEventArgs e) +26 EPiServer.Web.FriendlyUrlRewriteProvider.TryConvertToInternal(UrlBuilder url, LanguageApiMode apiMode, CultureInfo& preferredCulture, Object& internalObject) +173 EPiServer.Web.FriendlyUrlRewriteProvider.TryConvertToInternal(UrlBuilder url, CultureInfo& preferredCulture, Object& internalObject) +23 EPiServer.Web.UrlRewriteModule.HttpUrlRewriteToInternal(UrlBuilder url) +483 EPiServer.Web.UrlRewriteModuleBase.BeginRequestEventHandler(Object sender, EventArgs e) +205 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171 |
Version Information: Microsoft .NET Framework Version:2.0.50727.5448; ASP.NET Version:2.0.50727.5420