0

I was wondering if it is possible to use the QueryRun object in c# and if so, which namespace do I import to use it, as I have a method which returns a QueryRun object in my AX class, which I call in my C# code like so:

CallStaticClassMethod("OnlineUsers", "findMultipleProducts", user, id);

findMultipleProducts is the method in question, I need to access it as a QueryRun object as I need to iterate through the products using the .next() method. Any help or examples would be appreciated.

Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
CallumVass
  • 11,288
  • 26
  • 84
  • 154

1 Answers1

3

Are you accessing Ax through BC.NET? If so here is a sample of how to use QueryRun from BC.NET:

    using (var ax = new Axapta())
    {
        ax.Logon(null, null, null, null);
        int tableId = ax.GetTableId("TaxTable");
        var query = ax.CreateAxaptaObject("Query");
        var qbd = (AxaptaObject)query.Call("addDataSource", tableId);

        var qr = ax.CreateAxaptaObject("QueryRun", query); 

        while ((bool)qr.Call("next"))
        {
            var record = (AxaptaRecord)qr.Call("Get", tableId); 

            Console.WriteLine("TaxCode: {0}", record.get_Field("TaxCode"));
            Console.WriteLine("TaxName: {0}", record.get_Field("TaxName"));
        }
        ax.Logoff();
    } 

Where the GetTableId extension method is taken from this post:

    public static class AxaptaExtensions
    {
        public static int GetTableId(this Axapta ax, string table)
        {
            return (int)ax.CallStaticClassMethod("Global", "tableName2Id", table);
        }
    }
armasanea
  • 434
  • 3
  • 7
  • Hi, my Axapta object doesn't have a method called GetTableId. Check here: http://msdn.microsoft.com/en-us/library/microsoft.dynamics.businessconnectornet.axapta_methods.aspx – CallumVass Mar 13 '12 at 09:08
  • Yes, as I mentioned, the GetTableId is an extension method I've added to Axapta class. The code for it is in the blog post - I've now added to this post for your convenience. – armasanea Mar 13 '12 at 14:32
  • more on extension methods here: http://msdn.microsoft.com/en-us/library/bb383977.aspx – armasanea Mar 13 '12 at 14:39