Search code examples
c#axaptaax

Using QueryRun datatype in C#


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.


Solution

  • 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);
            }
        }