web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

SalesTable record via QueryService

(0) ShareShare
ReportReport
Posted on by 215

Hi,

I am attempting to retrieve a SalesOrder record in the SalesTable via the QueryService.

I am able to use ExecuteStaticQuery to return all the rows in the table via the SalesTableSelect query in AOT, but I cannot retrieve a single row using ExecuteQuery method.

Here is my C# code so far:

 

QueryServiceClient qsc = new QueryServiceClient();

Paging paging = null;

QueryMetadata query = new QueryMetadata();

QueryDataSourceMetadata salesOrderDataSource = new QueryDataSourceMetadata();

DataSet result;

 

query.Name = "SalesTableQuery";

query.DataSources = new QueryDataSourceMetadata[1];

salesOrderDataSource.Name = "SalesTable DataSource";

salesOrderDataSource.Enabled = true;

salesOrderDataSource.Table = "SalesTable";

salesOrderDataSource.DynamicFieldList = true;

salesOrderDataSource.DynamicFieldListSpecified = true;

query.DataSources[0] = salesOrderDataSource;

 

query.DataSources[0].Ranges = new QueryRangeMetadata[1];

QueryDataRangeMetadata range = new QueryDataRangeMetadata();

range.FieldName = "SalesId";

range.TableName = "SalesTable";

range.Enabled = true;

range.Value = mySalesId;

query.DataSources[0].Ranges[0] = range; 

                            

result = qsc.ExecuteQuery(query, ref paging);

Error:  Cannot find table 0

 

I know the SalesTable exists and I know the record with my SalesId exists.

Thank you for your help.

T. Brooks

*This post is locked for comments

I have the same question (0)
  • Joris dG Profile Picture
    17,780 on at

    Hi Trevor :-)

    Can you try remove the salesOrderDataSource.DynamicFieldListSpecified = true

  • Trevor Brooks Profile Picture
    215 on at

    Hi Joris!

    I did try that but it still returns the error: Cannot find table 0.   :-(

  • Trevor Brooks Profile Picture
    215 on at

    FYI If I take the range off it still doesn't return anything.

  • Martin Dráb Profile Picture
    239,099 Most Valuable Professional on at

    Your code (copy&paste) works for me (AX2012 R2). Could you provide more details about the exception (type, call stack)?

  • Trevor Brooks Profile Picture
    215 on at

    Hi Martin!

    We are on AX 2012 CU3.

    Stack trace:

      at System.Data.DataTableCollection.get_Item(Int32 index)

      at CLGSalesOrderWeb.SalesOrderPage.Page_Load(Object sender, EventArgs e) in C:\Users\tbrooks\documents\visual studio 2010\Projects\CLGSalesOrderWeb\CLGSalesOrderWeb\SalesOrderPage.aspx.cs:line 336

    Type:

    System.IndexOutOfRangeException

    The error occurs after the call to ExecuteQuery, when attempting to iterate the resulting dataset with this code:

                                       for (int ix = 0; ix < dataset.Tables[0].Rows.Count; ix++)

                                       {

                                           DataRow datarow = dataset.Tables[0].Rows[ix];

                                       }

    I also see this in the exception:

    _COMPlusExceptionCode = -532462766

    Thank you Martin!

  • Trevor Brooks Profile Picture
    215 on at

    Is there a way to see what actually executed in AX?  I'm pretty sure the query is making it to AX, it's just not returning anything.

  • Joris dG Profile Picture
    17,780 on at

    Hi Trevor,

    I didn't realize your execute actually returned, I thought that threw the error. I know what the issue is, you're not specifying your company so the service will take the default company of your user's profile. Inside AX, in your user options, what is your default company? I bet it's DAT or some other company with no data, which is why it's not finding any records.

  • Martin Dráb Profile Picture
    239,099 Most Valuable Professional on at

    I also understood it threw the exception somewhere in the code posted in the question, not that it returns valid but empty DataSet. I agree with Joris that wrong data area is the most common source of the problem.

    You could use Event Tracing to see what's hapenning inside AX.

  • Trevor Brooks Profile Picture
    215 on at

    Hi,

    I did look at my user info and I am set to the proper company, however, I would like to set up Event Tracing to see exactly what's happening on the AX side. (Not sure how to do that.)

    I also added these lines:

                                   query.AllowCrossCompany = true;

                                   query.AllowCrossCompanySpecified = true;

    But to no avail, still doesn't return any rows.

    Please help troubleshoot further, I know we are almost there.

    Regards,

    T. Brooks

  • Martin Dráb Profile Picture
    239,099 Most Valuable Professional on at

    AX supports Event Tracing for Windows (ETW) and allows you to trace many events, including those about X++ and SQL calls. Find details in Windows Event Tracing in Microsoft Dynamics AX 2012. Trace Parser may help you understanding the trace data. I've never tried to trace query service, so I can't tell you exactly what you'll get.

    Another option is to trace SQL commands using SQL Server Profiler.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Basit Profile Picture

Basit 1

#1
GL-01081504-0 Profile Picture

GL-01081504-0 1

#1
Roya Profile Picture

Roya 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans