Trikks

The digital adventures of Eric Herlitz

Archive for December, 2011

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

 

Posted in EpiServer Commerce, Mediachase | Leave a Comment »

Getting the previous and next record from list using linq

Posted by trikks on December 1, 2011

Generic lists, you just gotta love them, especially if using linq!

Well to the point. Lets say you got a list with Guid’s of which you know one and based on that knowledge want to get either the one before or after.

Guid a = new Guid("709DBFA9-88B3-4518-A32F-C21B6AAE2917");
Guid b = new Guid("B9956FC6-FD9B-4F52-B96E-C1D0C29E1E55");
Guid c = new Guid("BFB9054A-AB18-4349-BCB4-19979D3486BB"); // <-- you know this one, the c guid
Guid d = new Guid("1A8D6CAB-FD42-4517-A0EA-08D3F64DCD69");
Guid e = new Guid("02A8CA8A-5BC8-41BE-8AB8-332EF0B5407B");
List<Guid> guidList = new List<Guid> {a, b, c, d, e};

Solution

Oh wow, now I need to start building indexes pushing and popping yada yada. Well no, a better way to solve this is by using linq.
Getting the record after the one you search for

private static Guid GetNext(IEnumerable<Guid> guidList, Guid current)
{
    return guidList.SkipWhile(i => !x.Equals(current)).Skip(1).First();
}
And to get the record before...
private static Guid GetPrevious(IEnumerable<Guid> guidList, Guid current)
{
    return guidList.TakeWhile(i => !x.Equals(current)).Last();
}

Using Generics (you should)

Whilst separated methods can be neat sometimes a lot of time and engineering can be saved using some generics instead, this means you can dynamically change the type of list you want to search for a certain record.

In example

private static T GetNext<T>(IEnumerable<T> list, T current)
{
    try
    {
        return list.SkipWhile(x => !x.Equals(current)).Skip(1).First();
    }
    catch
    {
        return default(T);
    }
}

private static T GetPrevious<T>(IEnumerable<T> list, T current)
{
    try
    {
        return list.TakeWhile(x => !x.Equals(current)).Last();
    }
    catch
    {
        return default(T);
    }
}

Some usage

String example

string f = "first";
string g = "second";
string h = "third";
string i = "fourth";
string j = "fifth";
List<string> stringList = new List<string>{f,g,h,i,j};

var prev = GetPrevious<string>(stringList, j);
var next = GetNext<string>(stringList, j);

Guid example

Guid a = new Guid("709DBFA9-88B3-4518-A32F-C21B6AAE2917");
Guid b = new Guid("B9956FC6-FD9B-4F52-B96E-C1D0C29E1E55");
Guid c = new Guid("BFB9054A-AB18-4349-BCB4-19979D3486BB");
Guid d = new Guid("1A8D6CAB-FD42-4517-A0EA-08D3F64DCD69");
Guid e = new Guid("02A8CA8A-5BC8-41BE-8AB8-332EF0B5407B");
List<Guid> guidList = new List<Guid> {a, b, c, d, e};

var prev = GetPrevious<Guid>(guidList, c);
var next = GetNext<Guid>(guidList, c);

-

Cheers!

Posted in C# | 2 Comments »

 
Follow

Get every new post delivered to your Inbox.