Snowflake data binding in React Pivot Table
This guide explains how to retrieve data from a Snowflake database using the Snowflake.Data library and bind it to the Pivot Table through a Web API controller.
Creating a Web API Service to Fetch Snowflake Data
Follow these steps to create a Web API service that retrieves data from a Snowflake database and prepares it for the Pivot Table.
Step 1: Create an ASP.NET Core Web Application
- Open Visual Studio and create a new ASP.NET Core Web App project named MyWebService.
- Follow the official Microsoft documentation for detailed instructions on creating an ASP.NET Core Web application.

Step 2: Install the Snowflake NuGet Package
To enable Snowflake database connectivity:
- Open the NuGet Package Manager in your project solution and search for Snowflake.Data.
- Install the Snowflake.Data package to add Snowflake support.

Step 3: Create a Web API Controller
- Under the Controllers folder, create a new Web API controller named PivotController.cs.
- This controller facilitates data communication between the Snowflake database and the Pivot Table.
Step 4: Connect to Snowflake, Retrieve Data, and Serialize to JSON
In the PivotController.cs file, use the Snowflake.Data library to connect to a Snowflake database, retrieve data, and return it as JSON for the Pivot Table.
-
Establish Connection: Use SnowflakeDbConnection with a valid connection string. Snowflake requires
account=,warehouse=,db=, andschema=. Example:account=myaccount;user=myuser;password=mypassword;db=SAMPLE_DB;schema=PUBLIC;warehouse=COMPUTE_WH;role=PUBLIC; -
Authentication: Password authentication is shown above. For SSO, add
authenticator=externalbrowser. For key-pair authentication, omitpassword=and addauthenticator=snowflake_jwtplusprivate_key_file=...andprivate_key_file_pwd=.... - Query and Fetch Data: Execute a SQL query using SnowflakeDbDataAdapter to retrieve data for the Pivot Table.
- Structure the Data: Use SnowflakeDbDataAdapter’s Fill method to populate query results into a DataTable.
- Serialize to JSON: Use JsonConvert.SerializeObject to convert the DataTable into a JSON string for the Pivot Table.
Sample dataset: The query below uses the
CALL_CENTERtable from Snowflake’sSNOWFLAKE_SAMPLE_DATA.TPCDS_SF10TCLshared database. If your account does not have the sample data shared, replace the database/schema/table with your own.
using Microsoft.AspNetCore.Mvc;
using Snowflake.Data.Client;
using Newtonsoft.Json;
using System.Data;
namespace MyWebService.Controllers
{
[ApiController]
[Route("[controller]")]
public class PivotController : ControllerBase
{
[HttpGet(Name = "GetSnowflakeResult")]
public object Get()
{
return JsonConvert.SerializeObject(FetchSnowflakeResult());
}
public static DataTable FetchSnowflakeResult()
{
using (SnowflakeDbConnection snowflakeConnection = new SnowflakeDbConnection())
{
// Replace with your own connection string.
snowflakeConnection.ConnectionString = "<Enter your valid connection string here>";
snowflakeConnection.Open();
SnowflakeDbDataAdapter adapter = new SnowflakeDbDataAdapter("select * from CALL_CENTER", snowflakeConnection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
snowflakeConnection.Close();
return dataTable;
}
}
}
}Ensure the Newtonsoft.Json NuGet package is installed in your project to use
JsonConvert.
Step 5: Enable CORS in the Web API
React (typically http://localhost:3000 or http://localhost:5173) running on a different origin than the Web API will be blocked by CORS unless the API explicitly allows it. In Program.cs, register and apply a CORS policy:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowReactApp", policy =>
policy.WithOrigins("http://localhost:3000", "http://localhost:5173")
.AllowAnyHeader()
.AllowAnyMethod());
});
var app = builder.Build();
app.UseCors("AllowReactApp"); // Must be called before MapControllers.
app.MapControllers();
app.Run();Step 6: Run the Web API Service
- Build and run the application.
- The application will be hosted at
https://localhost:7150/by default (the port number is defined in Properties/launchSettings.json and may vary based on your configuration).
Step 7: Access the JSON Data
- Access the Web API endpoint at
https://localhost:7150/Pivotto view the JSON data retrieved from the Snowflake database. - The browser will display the JSON data, as shown below.

Connecting the Pivot Table to a Snowflake Database Using the Web API Service
This section explains how to connect the Pivot Table component to a Snowflake database by retrieving data from the Web API service created in the previous section.
Step 1: Create a Pivot Table in React
- Set up a basic React Pivot Table by following the Getting Started documentation.
- Ensure your React project is configured with the necessary EJ2 Pivot Table dependencies.
Step 2: Configure the Web API URL in the Pivot Table
- In the App.tsx (or App.jsx) file, map the Web API URL (
https://localhost:7150/Pivot) to the Pivot Table using the url property within the dataSourceSettings. - Below is the sample code to configure the Pivot Table to fetch data from the Web API:
import { PivotViewComponent, FieldList, Inject } from '@syncfusion/ej2-react-pivotview';
import * as React from 'react';
import './App.css';
function App() {
let dataSourceSettings = {
url: 'https://localhost:7150/Pivot'
// Additional configuration will be added in the next step
};
return (<PivotViewComponent id='PivotView' height={350} dataSourceSettings={dataSourceSettings} showFieldList={true}>
<Inject services={[FieldList]}/>
</PivotViewComponent>);
};
export default App;Step 3: Define the Pivot Table Report
- Configure the Pivot Table report in the App.tsx (or App.jsx) file to structure the data retrieved from the Snowflake database.
- Add fields to the rows, columns, values, and filters properties of dataSourceSettings to define the report structure, specifying how data fields are organized and aggregated in the Pivot Table.
- Enable the field list by setting the showFieldList property to true and including the
FieldListmodule in the services section. This allows users to dynamically add or rearrange fields across the columns, rows, and values axes using an interactive user interface.
Here’s the updated sample code for App.jsx with the report configuration and field list support:
import { PivotViewComponent, FieldList, Inject } from '@syncfusion/ej2-react-pivotview';
import * as React from 'react';
import './App.css';
function App() {
let dataSourceSettings = {
url: 'https://localhost:7150/Pivot',
enableSorting: true,
expandAll: false,
columns: [
{ name: 'CC_COUNTRY', caption: 'Country' }
],
rows: [
{ name: 'CC_STATE', caption: 'State' },
{ name: 'CC_CITY', caption: 'City' }
],
values: [
{ name: 'CC_COMPANY', caption: 'Company' },
{ name: 'CC_EMPLOYEES', caption: 'Employees' },
{ name: 'CC_TAX_PERCENTAGE', caption: 'Percentage' }
],
filters: []
};
return (<PivotViewComponent id='PivotView' height={350} dataSourceSettings={dataSourceSettings} showFieldList={true}>
<Inject services={[FieldList]}/>
</PivotViewComponent>);
};
export default App;Step 4: Run and Verify the Pivot Table
- Run the React application.
- The Pivot Table will display the data fetched from the Snowflake database via the Web API, structured according to the defined report.
- The resulting Pivot Table will look like this:

Additional Resources
Explore a complete example of the React Pivot Table integrated with an ASP.NET Core Web Application to fetch data from a Snowflake database in this GitHub repository.