Export (0) Print
Expand All

Working with Data for Enterprise Portal

Dynamics AX 2009

The code you add for User Controls will often be required to work with data for Enterprise Portal. Several techniques can be used to access data for Enterprise Portal. These are:

  • .NET Business Connector

  • Application Proxy

  • Data Sets and AxDataSource

Which you choose depends on the specific requirements for the User Control you are creating.

Code for User Controls can access data through the .NET Business Connector. To access the .NET Business Connector, you must include the following using statements to reference the appropriate namespaces.

using Microsoft.Dynamics.Framework.BusinessConnector.Session;
using Microsoft.Dynamics.Framework.BusinessConnector.Adapter;

You must also use the Session object to connect to the .NET Business Connector. This object is described in Session Object. For details about how to use the .NET Business Connector to access data, see .NET Business Connector Overview.

The following example shows how to use the .NET Business Connector from code for a User Control. This example tries to retrieve a row from the AddressState table in Microsoft Dynamics AX. If the row is successfully retrieved, the state name is displayed in the StateName text field for the User Control. Notice that the Session object is used to connect to the .NET Business Connector. No separate logon action is needed.

IAxaptaRecordAdapter axRecord;

using( axRecord = AxSession.AxaptaAdapter.CreateAxaptaRecord("AddressState"))
{
    // Try to retrieve the record for Wyoming.
    axRecord.ExecuteStmt("select * from %1 where %1.StateId=='WY'");

    if (axRecord.Found)
    {
        // Display the name of the state.
        StateName.Text = axRecord.GetField("NAME").ToString();
    }
}
ImportantImportant

Notice that a using block encloses the code that works with the IAxaptaRecordAdapter. Use this pattern to make sure that the resources for the IAxaptaRecordAdapter will be freed when it goes out of scope.


If you have X++ code that performs data access needed by the User Control you are creating, you may want to use the application proxy to access that code. For details about how to use the application proxy, see Proxies.

Much of the data displayed in Enterprise Portal is accessed through data set resources defined in the AOT. For more information about data sets, see Data Sets for Enterprise Portal. AxDataSource components added to the layout for User Controls are used to access data sets. Other User Control components such as the AxGridView and AxForm access data through the AxDataSource components. Code you add for User Controls can also access data through the AxDataSource components. To use the classes and methods that are required to work with AxDataSource components, add the following using statement to your User Control code.

using Microsoft.Dynamics.Framework.Data.Ax;

Returning the Views for an AxDataSource

A data set can have several views. Usually, these views correspond to the tables that are used to create the data set. Each data source for the data set has two views. One view represents all the rows that can be accessed through the view. The other view represents the current row, and has a name that ends with the string _Current.

The following example shows how to retrieve the views for the EmployeeDS AxDataSource component. The views are added to a list when the user clicks the button.

protected void GetViews_Click(object sender, EventArgs e)
{
    // Clear the list of views.
    Views_ListBox.Items.Clear();

    // Add the views for the data source to the list.
    foreach (string s in EmployeeDS.GetDataSetViewNames())
    {
        Views_ListBox.Items.Add(s);
    }
}

Returning the Current Row for an AxDataSource

In code for User Controls, you may want to access the current row for an AxDataSource. The current row is often set by the component linked to the AxDataSource, such as an AxGridView. The following example is a function that retrieves the current row from the EmployeeDS data source, which is accessing the EPEmplTableList data set. The view named EmplTable is used to access the data source.

private DataSetViewRow CurrentRow
{
    get
    {
        try
        {
            DataSetView dataSetView;
            dataSetView = this.EmployeeDS.GetDataSet().DataSetViews["EmplTable"];
            return (dataSetView == null) ? null : dataSetView.GetCurrent();
        }
        catch (System.Exception)
        {
            return null;
        }
    }
}

Getting and Setting Field Values for a Row

To get or set the values for fields in a row, use the GetFieldValue or SetFieldValue methods for the DataSetViewRow object. The following example retrieves the value of the EmplId field for the current row in the Vehicles data source.

DataSetViewRow row;
string employeeID;

// Retrieve the current row.
row = VehiclesDS.GetDataSet().DataSetViews["Vehicles"].GetCurrent();
// Retrieve the EmplId value.
employeeID = row.GetFieldValue("EmplId").ToString();

The following code edits the current row in the Vehicles data source. The value of the EmplId field is set, and then the changes to the row are saved.

DataSetViewRow row;

try
{
    // Retrieve the current row.
    row = VehiclesDS.GetDataSet().DataSetViews["Vehicles"].GetCurrent();

    // Edit the row and change the EmplId value.
    row.BeginEdit();
    row.SetFieldValue("EmplId", "ASR");
    row.EndEdit();
}
catch (System.Exception ex)
{
    AxExceptionCategory exceptionCategory;

    // Determine whether the exception can be handled.
    if (AxControlExceptionHandler.TryHandleException(this, ex, out exceptionCategory) == false)
    {
        // The exception was fatal and cannot be handled. Rethrow it.
        throw;
    }
}

Custom Data Set for a Lookup

When adding a lookup component to a User Control, you may want to access data for which no data set has been defined in the AOT. You can use C# code to create a custom data set for the lookup. For an example of this, see Lookups.

Community Additions

ADD
Show:
© 2015 Microsoft