Trikks

The digital adventures of Eric Herlitz

EPiServer Commerce (Mediachase) Unable to cast object of type ‘System.DBNull’ to type ‘System.String’. after creating a new catalog

Posted by trikks on December 22, 2011

This means you (or someone) where sloppy when creating a new catalog. the error may vary but check the InvalidCastException in the stacktrace for hints.

In this scenario the catalog didn’t have a default language set, so removing it and adding that property when creating it solved the issue.

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

 

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: