DEV Community

Cover image for How to Fetch Adobe Commerce Data with a C# .NET Magento Connector
Chelsea Devereaux for MESCIUS inc.

Posted on • Originally published at developer.mescius.com

How to Fetch Adobe Commerce Data with a C# .NET Magento Connector

What You Will Need

  • Visual Studio 2022
  • ComponentOne Data Services Edition

Controls Referenced

Tutorial Concept

Learn how to fetch data from an online Adobe Commerce (also known as Magento) database and display the data within a .NET application using C#.


Are you looking to retrieve data from your Magento Server to build a .NET app? You’re at the right place.

As technology changes daily, there are several sources to store data, like the Magento server. Adobe provides an open-source platform named Magento that is used to create e-commerce stores and handle complex operations. If you plan to develop a .NET app using data from a Magento server, then fetching this data might seem a bit complicated. However, our ComponentOne Data Connector suite features an API called ADO.NET provider for Magento, which offers several classes to streamline connectivity with Magento.

In this blog, we will develop a WinForms app using Magento e-commerce data. Let's break down the process into the following easy steps:

Setup a WinForms App with Required Dependencies

Let's begin by setting up a new .NET 8 WinForms app that includes the ADO.NET Provider for Magento dependency by following these steps:

  1. Open Visual Studio and select File | New | Project to create a new WinForms app.

C# .NET Adobe Commerce_Configure

2.Right-click on the project in Solution Explorer and choose Manage NuGet Packages… from the context menu.

C# .NET Adobe Commerce_Manage Packages

3.Search for C1.AdoNet.Magento in the NuGet Package Manager and click on Install.

C# .NET Adobe Commerce_Install Package

To create the app UI, we will use the input and datagrid controls from the ComponentOne WinForms suite. So, let’s add the following NuGet packages:

Now that we have successfully set up the environment, the next step is to establish a connection to the Magento Server to fetch the data.

Fetch Data from the Magento Server

To fetch the data using the C1.AdoNet.Magento API, we need to start by establishing the connection. We are using the C1MagentoConnectionStringBuilder class to create the connection string by setting its Url, Username, Password, and TokenType properties:

    //Create a connection string using C1MagentoConnectionStringBuilder
    C1MagentoConnectionStringBuilder connBuilder= new C1MagentoConnectionStringBuilder();
    connBuilder.Username = @”******";
    connBuilder.Password = @"*******";
    connBuilder.TokenType= @"****";
    connBuilder.Url = @"http://****/****/****";
Enter fullscreen mode Exit fullscreen mode

Note: You can pass your Magento credential in the code above.

Once the connection string is ready, let’s pass it to the C1MagentoConnection class to open the connection with your Magento database:

    //Create and establish connection
    C1MagentoConnection conn = new C1MagentoConnection(connBuilder)
    //Open connection with Database
    conn.Open();
Enter fullscreen mode Exit fullscreen mode

Note: You can also check out more connection-building techniques here.

Now, the connection has been made successfully with the Magento server. Let’s create an adapter using the C1MagentoDataAdapter class to retrieve the data into the DataTable based on the specified query.

    //Populate DataTable
    C1MagentoDataAdapter adapter = new C1MagentoDataAdapter(conn, "Select * from Products");
    DataTable dt = new DataTable();
    adapter.Fill(dt);
Enter fullscreen mode Exit fullscreen mode

Note: You can also check out more data query techniques here.

We have successfully fetched the data from the Magento server. Next, we will bind it with the UI to make it presentable.

Create a UI and Bind It with the Fetched Magento Data

Initially, we added the FlexGrid and InputPanel packages to the project. Now, let’s place them onto the form to make a UI that aligns with the data fields. Here’s how we designed the form:

C# .NET Adobe Commerce_Create UI

Let’s bind the grid and input controls with the DataTable using DataSource and DataField properties:

    c1FlexGrid1.DataSource = dt;
    inputDataNavigator1.DataSource = dt;
    inputTextBox1.DataSource = dt;
    inputTextBox1.DataField = "id";
    inputTextBox2.DataSource = dt;
    inputTextBox2.DataField = "name";
    inputTextBox3.DataSource = dt;
    inputTextBox3.DataField = "attribute_set_id";
    inputTextBox4.DataSource = dt;
    inputTextBox4.DataField = "sku";
    inputTextBox5.DataSource = dt;
    inputTextBox5.DataField = "type_id";
    inputDatePicker1.DataSource = dt;
    inputDatePicker1.DataField = "created_at";
    inputDatePicker2.DataSource = dt;
    inputDatePicker2.DataField = "updated_at";
Enter fullscreen mode Exit fullscreen mode

That’s it! We have successfully created a WinForms app that showcases the data from the Magento server and works as shown below. You can even insert and delete records.

DataConnectors_Magento_Sample

Download the sample and update it with your credentials to try it out.

Conclusion

In this blog, we showcased easy data connection techniques in .NET from the Magento server. With the data connector API, you can fetch the data from various sources like Salesforce, Kintone, and OData, among others. Explore the Data Connector documentation to learn more about these powerful APIs.

Top comments (0)