Developer forum

Forum » Development » DW 8 API: Is it possible to get items with SQL?

DW 8 API: Is it possible to get items with SQL?

Marie Louise Veigert
Reply

Hi DW,

I'm developing a ItemTemplateExtender, where I need some items.

I've tried to get the item with 'Paragraph.GetParagraphsByPageID' followed by 'ItemManager.Storage.GetByParagraphId' which work in a normal .cs, called from a eCom product template, but NOT if the .cs is called from the Extender.

So I wondered, if it's possible to get the items from the database with SQL like when you use 'Product.GetProductsBySql' with products?

DW version: 8.8.1.32

/Marie Louise


Replies

 
Nicolai Pedersen
Reply

Hi Marie

You are doing it right - I cannot see why it should not work. You write it does not work - how is that? Do you get an exception, or something wrong - what happens? And can you post your code please.

BR Nicolai

 
Marie Louise Veigert
Reply

It through an 'object cannot be null' exception, when I debug. In the 'FindBrokerItem' method I can see it finds items but still fails, when it returns.

Extender code, where it fails in 'List<Item> i = itemhandler.FindBrokerItem(id, "Medarbejder", ImportedMedarbejderID)':

FindItemHandler itemhandler = new FindItemHandler();
List<Item> employees = new List<Item>();

foreach (string id in brokerIdList)
{
      List<Item> i = new List<Item>();
      i = itemhandler.FindBrokerItem(id, "Medarbejder", ImportedMedarbejderID);

      if (i.Count > 0)
      {
               employees.Add(i[0]); 
      }
}

 

--------------------------------------------

FindBrokerItem method:

 public List<Item> FindBrokerItem(string brokerId, string itemType, int pageID)
        {
           List<Item> itemList = new List<Item>();

            var paragraphs = Paragraph.GetParagraphsByPageID(pageID).Where(x => x.ShowParagraph);

            foreach (Paragraph p in paragraphs)
            {
                var items = ItemManager.Storage.GetByParagraphId(itemType, p.ID).Where(x => x["BrokerID"].ToString() == brokerId);

                    foreach (var item in items)
                    {
                       itemList.Add(item);
                       break;
                    }
                if (itemList.Any())
                {
                    break;
                }
            }
            return itemList;
        }

 
Nicolai Pedersen
Reply

So which line throws object cannot be null exception? And what happens if you null check that object that happens to be null?

 
Marie Louise Veigert
Reply

When I use Try/catch around:

 List<Item> i = new List<Item>();
      i = itemhandler.FindBrokerItem(id, "Medarbejder", ImportedMedarbejderID);

      if (i.Count > 0)
      {
               employees.Add(i[0]); 
      }

the exception '{"Object reference not set to an instance of an object."}' is thrown.

But if its possible to get an item from the database by SQL instead i think it will work. 

The exception isn't thrown when I use the exact same code in the eCom template (the code below is from the template, where it works):

FindItemHandler i = new FindItemHandler();
List<Item> employeeList = i.FindBrokerItem(GetString("Ecom:Product:Field.BrokerIdField.Value"), "Medarbejder", ImportedMedarbejderID);

 
Marie Louise Veigert
Reply

And it's the ' i = itemhandler.FindBrokerItem(id, "Medarbejder", ImportedMedarbejderID);' line which cause the exception.

 
Nicolai Pedersen
Reply

Which of the objects are null?

And why parse ImportedMedarbejderID to FindBrokerItem where it takes a pageid?

You solution is not to use SQL directly - it is to find out what is null and why...

 
Marie Louise Veigert
Reply

The debugger doesn't tell me directly. See attached.

ImportedMedarbejderID is the pageID, where the items are stored.

Exception.png
 
Nicolai Pedersen
Reply

If mouseover the variables in the break point, you can see it...

 
Marie Louise Veigert
Reply

After trying a try/catch in FindBrokerItem I found the cause. Thanks for the help :)

 
Nicolai Pedersen
Reply

Great!

Those null reference exceptions are always træls!

Happy coding, Nicolai

 

You must be logged in to post in the forum