locked
How can I use a @Helper within a @foreach loop? RRS feed

  • Question

  • User1122679459 posted

    I'm having some trouble using a @Helper I wrote within a loop but  I'm getting the following error:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Here is the code:

    @foreach (var item in Products) {
    
      @Helpers.CheckPrice(item.ItemID);
    
    } 

    Wednesday, March 4, 2015 8:54 PM

Answers

  • User-1461986491 posted

    System.NullReferenceException error occured whenerver you can convert null value into different types.

    For example, If you can convert null value to string type using ToString extension method then it will through System.NullReferenceException error.

    So check null value before proceed.

    if(value != null){
     //proceed code for value..
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2015 11:59 PM

All replies

  • User-1461986491 posted

    System.NullReferenceException error occured whenerver you can convert null value into different types.

    For example, If you can convert null value to string type using ToString extension method then it will through System.NullReferenceException error.

    So check null value before proceed.

    if(value != null){
     //proceed code for value..
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2015 11:59 PM
  • User-735851359 posted

    Hello,

    Can you show the code for SQL where you get ItemID?

    Thursday, March 5, 2015 4:39 AM
  • User1122679459 posted

    This is the code I'm using for ItemID

    var db = Database.Open("data");
    var Products = db.Query("SELECT * FROM Products");

    Thursday, March 5, 2015 6:58 AM
  • User325035487 posted
    if(products.Any()){
     //proceed code for value..
    }

    http://www.mikesdotnetting.com/article/214/how-to-check-if-a-query-returns-data-in-asp-net-web-pages

    Thursday, March 5, 2015 1:11 PM
  • User-735851359 posted

    There is no error in the given SQL code.

    So you have the mistake in Helper. Show your Helper code please..

    Thursday, March 5, 2015 2:32 PM
  • User1122679459 posted

    This is the helper code:

    @helper CheckPrice(string itemID)
    {
        var requestUrl = Functions.Service(itemID);
    
        XNamespace ns = "http://webservices.a.com/Service/2013-08-01";
        var xml = XDocument.Load(requestUrl);
    
        var items = xml.Descendants(ns + "Items").Descendants(ns + "Item").Select(item => new
        {
            ListPrice = item.Element(ns + "ItemAttributes").Element(ns + "ListPrice").Element(ns + "FormattedPrice").Value,
            DiscountPrice = item.Element(ns + "OfferSummary").Element(ns + "LowestNewPrice").Element(ns + "FormattedPrice").Value
        });
    
        <ol>
            @foreach (var item in items)
            {
                <li>@item.ListPrice</li>
                <li>@item.DiscountPrice</li>
            }
        </ol>
    }

    Thursday, March 5, 2015 9:26 PM