Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 26, 2010 @ 15:59
    Dan Diplo
    0

    Linq To Umbraco 4.5.2 - Exceptions with AssociationTree<>

    Been playing around with Umbraco Linq in 4.5.2 and am impressed with the code-gen and the concept. It's going to be immense! But I've (inevitably) got a problem... I created two classes (interfaces and concrete) using the export function on DocTypes.

    All works well when enumerating a document type. For instance, this code works great:

    using (MyUmbracoDataContext uContext = new MyUmbraco.MyUmbracoDataContext())
    {
    foreach (var item in uContext.Articles)
    {
    Response.Write(item.NodeName + "<br />");
    }
    }

    However, if I try and access the type's children then I always get an exception. Both the following statements will cause a FormatException:

    foreach (var item in uContext.Articles)
    {
    int count = item.Articles.Count();
    int children = item.Children.Count();
    }

    The exception that is raised is:

    [FormatException: Input string was not in a correct format.]
       System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7471479
       System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
       System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +373
       System.Convert.ChangeType(Object value, Type conversionType) +32
       umbraco.Linq.Core.Node.NodeDataProvider.LoadFromXml(XElement xml, T node) +2415
       umbraco.Linq.Core.Node.<DynamicNodeCreation>d__6.MoveNext() +468
       System.Linq.WhereEnumerableIterator`1.MoveNext() +191
       System.Linq.<CastIterator>d__aa`1.MoveNext() +181
       System.Linq.Enumerable.Count(IEnumerable`1 source) +213
       umbraco_LinqTest.Page_Load(Object sender, EventArgs e) in d:\xxx\xxxx\WWW\umbraco\LinqTest.aspx.cs:17
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +50
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
     

    This happens for all AssociationTree<> types for every document type, not just Article.

    The generated IMaster interface looks like this:

    public partial interface IMaster : IDocTypeBase
    {
    String HeadingText { get; set; }
    String SubHeading { get; set; }
    String Tags { get; set; }
    String BodyText { get; set; }
    Int32? Image { get; set; }
    String CssClass { get; set; }
    Int32? QuotePageId { get; set; }
    String ImageUrl { get; set; }
    String RelatedLinks { get; set; }
    Int32? LastFmEvents { get; set; }
    String BrowserTitle { get; set; }
    String SummaryText { get; set; }
    String CalaisMetaData { get; set; }
    Int32? UmbracoNaviHide { get; set; }
    Int32? HidePage { get; set; }
    String UmbracoUrlName { get; set; }
    }

    And the IArticle interface looks like this:

    public partial interface IArticle : IMaster
    {
    IEnumerable<IArticle> Articles { get; }
    IEnumerable<IContactForm> ContactForms { get; }
    IEnumerable<IGallery> Gallerys { get; }
    IEnumerable<ILinksList> LinksLists { get; }
    IEnumerable<IListingPage> ListingPages { get; }
    IEnumerable<ILoveFilm> LoveFilms { get; }
    IEnumerable<IMp3Player> Mp3Players { get; }
    IEnumerable<IMusicPage> MusicPages { get; }
    IEnumerable<IQuotes> Quotess { get; }
    IEnumerable<IRSSFeedListing> RSSFeedListings { get; }
    IEnumerable<IYouTubeListing> YouTubeListings { get; }
    }

     Any help appreciated! If you need examples of any XML or whatever I can supply them. Thanks.

  • Eduardo 106 posts 130 karma points
    Nov 26, 2010 @ 16:12
    Eduardo
    0

    Hi Dan Diplo,

     

    foreach (var item in uContext.Articles)
    {
           
    int count = int.Parse(item.Articles.Count());
           
    int children = int.Parse(item.Children.Count());
    }

    HTH

    Sincere regards,
    Eduardo

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 26, 2010 @ 16:27
    Dan Diplo
    0

    Thanks, Eduardo, but Count() returns an Int32 so doesn't require parsing. But it's not the Count() part that causes the exception, it is the access to the collection. Getting the Count() was just an example of making Linq enumerate the child collection.  For instance, the following lines will also cause the exception:

    foreach (var child in item.Children)
    {

    }

    var articles = item.Articles.ToList();

    Basically, as soon as the children are enumerated in any way the exception is raised.

  • Eduardo 106 posts 130 karma points
    Nov 26, 2010 @ 16:54
    Eduardo
    0

    Hi Dan,

    You are right.

    Put the code inside a try catch.

    try{
    foreach
    (var child in item.Children){}
    var
    articles = item.Articles.ToList();
    }
    catch(Exception ex){}
    finally{}


    Check if child is not null before accesing it.

    HTH

    Sincere regards,
    Eduardo

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 26, 2010 @ 22:05
    Aaron Powell
    0

    The problem is that one of the Int-based properties has a value which is non-Int in the XML cache.

    When LINQ to Umbraco is setting property values it does a few things:

    - Checks if the property type is a value type (such as int)

    - if the value in the XML is null (or empty) and the value type is Nullable<T> assign null to it

    - if the value in the XML isn't null convert the value into an int and then set it to the property (or Nullable<T>.Value)

    So the error is happening when it converts the value into an int to then set onto the property. Check the XML and ensure that all the data is either blank or a valid number

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 30, 2010 @ 12:52
    Dan Diplo
    0

    Hi Slace. Thanks for your input. I'd guessed it was probably something like this, the problem was identifying which of the many doc types was at fault! I eventually managed to identify the one and changed the Int32's to Strings and this indeed fixed my problem.

    I can totally understand that automated code-generation can't always get this right, but wonder if it would be possible to include more diagnostics to help find any properties that fail conversion? This would be handy with complex, real-world sites.

    But let me also say that even as it currently is, Linq to Umbraco is very impressive - so many thanks for all your hard work.

  • Martin Lingstuyl 202 posts 379 karma points
    Mar 03, 2011 @ 13:57
    Martin Lingstuyl
    0

    Hi people,

    So to summarise the linq2umbraco generator doesnt always generate the right stuff?

    I'm having the same kind of problems. In my case I have some custom datatypes that are set to string, but linq thinks that they are Int32's. I dont know how to change this.

    Should I just change the linq cs file manually? which is kind of dorky, right...

    Do you have an idea slace?

    Martin

Please Sign in or register to post replies

Write your reply to:

Draft