2

I am stumped on how a foreach loop could throw an index out of range. I am working with the ebay api to get a list of items. I can verify with QuickWatch in Visual Studio that the collection contains 200 items.

I have searched for duplicate questions, but none matched my case.

My code to retrieve the list of items:

public ItemTypeCollection GetMyActiveItems()
{
    GetMyeBaySellingCall myEbaySelling = new    GetMyeBaySellingCall(ApiContext);

    myEbaySelling.ActiveList = new ItemListCustomizationType();
    myEbaySelling.ActiveList.Sort = ItemSortTypeCodeType.ItemID;
    myEbaySelling.UnsoldList = new ItemListCustomizationType();
    myEbaySelling.UnsoldList.Sort = ItemSortTypeCodeType.ItemID;
    myEbaySelling.SoldList = new ItemListCustomizationType();
    myEbaySelling.SoldList.Sort = ItemSortTypeCodeType.ItemID;

    myEbaySelling.Execute();
    var items = myEbaySelling.ActiveListReturn.ItemArray;
    //checking collection here. I can iterate this just fine. 
    // with no errors. 
    foreach (ItemType item in items)
    {
        var x = item.Title;
    }
    return items; 
}

Then on the Controller I am simply return passing the model to the View:

public ActionResult Items()
{
    ItemTypeCollection items = service.GetMyActiveItems();
    return View(items);
}

Then on the view, I am simply iterating the items to output to the browser:

@using eBay.Service.Core.Soap;

@model ItemTypeCollection


@foreach (ItemType item in Model)
{ // This is where the debugger stops on the first item with Index out of range exception. 
    <div class="col-sm-6 col-md-4">
        <div class="thumbnail">
        <h4 class="text-center"><span class="label label-info"></span></h4>
        <img src="@item.PictureDetails.PictureURL[0]" class="img-responsive">
         <div class="caption">
            <div class="row">
                <div class="col-md-6 col-xs-6">
                    <h3>@item.Title</h3>
                </div>
                <div class="col-md-6 col-xs-6 price">
                    <h3>
                        <label>@item.BuyItNowPrice.Value.ToString()</label>
                    </h3>
                </div>
            </div>
            <p>@item.SubTitle</p>
        </div>
    </div>
</div>
}
ekad
  • 14,436
  • 26
  • 44
  • 46
user1579943
  • 63
  • 1
  • 7
  • Comment out the foreach loop in the service's method. – t0mm13b Apr 28 '16 at 15:07
  • 5
    `item.PictureDetails.PictureURL` does that array always contain at least one item? at the moment there's an assumption it does – Matthew Mcveigh Apr 28 '16 at 15:08
  • Obviously. However, I put that in there to test (successfully) that I can iterate the loop without the Index out of range error. In the View is where the error occurs with or without the loop in the GetMyActiveItems() method. – user1579943 Apr 28 '16 at 15:09
  • I believe @MatthewMcveigh nailed it – Jonesopolis Apr 28 '16 at 15:11
  • Exactly what @MatthewMcveigh said. I've worked with the ebay API quite a bit and a possibility is that you need to specify a detail level that allows the return of a `PictureURL` array. The ebay API is finicky and annoying, especially the .NET one. This is unrelated but I **HIGHLY** recommend switching to the XML API, it is much more reliable, maintained and documented – Tyler Benzing Apr 28 '16 at 15:12
  • @Matthew I commented out the line for the PictureURL and it did load without the error. I suppose I was confused why it stopped on the bracket before it ever hit that line of code. – user1579943 Apr 28 '16 at 15:12
  • @UpAllNight I started with using the SDK from eBay. I understood the calls from the SDK was SOAP, which is XML. Am I missing something? – user1579943 Apr 28 '16 at 15:33
  • @user1579943 They are but you have to think. The XML API is going to have to be updated before the .NET one can be. Also, although they are the same API the documentation for the .NET SDK is almost non existent in comparison to the pure XML SOAP API – Tyler Benzing Apr 28 '16 at 15:40

2 Answers2

0

I think you want to check to make sure the picture details exist and there are picture urls

@if(item != null && item.PictureDetails != null && item.PictureDetails.PictureURL.Any())
{
   <img src="@item.PictureDetails.PictureURL.FirstOrDefault()" class="img-responsive">
} 
johnny 5
  • 19,893
  • 50
  • 121
  • 195
-1

var items looks like a multidimensional array, try using a simple for loop based on actual number of items.

Foreach will look for every element into the multidimensional array, including the ones that have no title property.

Damien
  • 40
  • 4