Snowflake Data Binding in ASP.NET MVC Pivot Table
28 Aug 202611 minutes to read
This section describes how to retrieve data from a Snowflake database using Snowflake Data and bind it to the Pivot Table via 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. Select the Web API project template (for example, ASP.NET Core Web API in Visual Studio 2022) so the project is configured with controllers and Swagger by default.
- 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. Use
Snowflake.Dataversion 2.x (or later) to match this walkthrough; pin the version if you want reproducible builds.

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 and Retrieve Data
In the PivotController.cs file, use the Snowflake.Data library to connect to a Snowflake database and retrieve data for the Pivot Table.
-
Establish Connection: Use SnowflakeDbConnection with a valid connection string (e.g.,
account=myaccount;user=myuser;password=mypassword;db=mydb;schema=myschema;) to connect to the Snowflake database. -
Query and Fetch Data: Execute a SQL query (e.g.,
SELECT * FROM CALL_CENTER) using SnowflakeDbDataAdapter to retrieve data for the Pivot Table. - Structure the Data: Use SnowflakeDbDataAdapter’s Fill method to populate query results into a DataTable for JSON serialization.
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;
}
}
}
}Step 5: Serialize Data to JSON
In the PivotController.cs file, define a Get method that calls FetchSnowflakeResult to retrieve data from the Snowflake database as a DataTable. Then, use JsonConvert.SerializeObject from the Newtonsoft.Json library to convert the DataTable into JSON format. This JSON data will be used by the Pivot Table component.
Ensure the
Newtonsoft.JsonNuGet package (version 13.x or later) is installed in your project before usingJsonConvert. TheGetmethod serializes theDataTableinto a JSON string before ASP.NET Core’s pipeline returns it as the response body. Note: returning aJsonConvert.SerializeObjectof aDataTableproduces a JSON array of row objects whose column values are mapped from the underlying Snowflake column types (for example,NUMBER→number,VARCHAR→string,TIMESTAMP_NTZ→ ISO-8601 string,VARIANT→ object).
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;
}
}
}
}Step 6: Run the Web API Service
- In Visual Studio, set MyWebService as the startup project and press F5 (or run
dotnet runfrom the project folder). The actual listening ports are read fromlaunchSettings.json; both HTTP and HTTPS endpoints are printed in the console. - The application will be hosted at
https://localhost:44378/(the port number may vary based on your configuration). Note the exact URL printed by the runtime so you can reference it from the ASP.NET MVC project.
Step 7: Access the JSON Data
- Access the Web API endpoint at
https://localhost:44378/Pivotto view the JSON data retrieved from the Snowflake database. - The browser displays the JSON data, as shown in the image below, ready for use by the Pivot Table.

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. Ensure that the Web API service from the previous section is still running before proceeding.
Step 1: Set up the ASP.NET MVC project
- Set up a basic ASP.NET MVC Pivot Table by following the Getting Started documentation.
- Ensure your ASP.NET MVC project is configured with the necessary EJ2 Pivot Table dependencies.
Step 2: Configure the Web API URL in the Pivot Table
- In the ~/Views/Home/Index.cshtml file, map the Web API URL (
https://localhost:44378/Pivot) to the Pivot Table using the Url property within the PivotViewDataSourceSettings. - Below is the sample code to configure the Pivot Table to fetch data from the Web API:
@Html.EJS().PivotView("PivotView").Height("300").DataSourceSettings(
dataSource => dataSource.Url("https://localhost:44378/pivot"
)
//Other codes here...
).Render()Step 3: Define the Pivot Table Report
- Configure the Pivot Table report in the ~/Views/Home/Index.cshtml file to structure the data retrieved from the Snowflake database.
- Add fields to the
rows,columns,values, andfiltersproperties of PivotViewDataSourceSettings 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 on the
PivotViewcomponent (not on the data source settings) and including theFieldListmodule 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. Note:EnableSortingis a property of thePivotViewcomponent; the sample above demonstrates the equivalent MVC builder usage for the data source settings.
Here is the updated sample code with the report configuration and field list support:
@Html.EJS().PivotView("PivotView").Height("300").DataSourceSettings(
dataSource => dataSource.Url("https://localhost:44378/Pivot"
).ExpandAll(false).EnableSorting(true)
.Rows(rows =>
{
rows.Name("CC_STATE").Caption("State").Add(); rows.Name("CC_CITY").Caption("City").Add();
}).Columns(columns =>
{
columns.Name("CC_COUNTRY").Caption("Country").Add();
}).Values(values =>
{
values.Name("CC_COMPANY").Caption("Company").Add(); values.Name("CC_EMPLOYEES").Caption("Employees").Add();
values.Name("CC_TAX_PERCENTAGE").Caption("Percentage").Add();
})).ShowFieldList(true).Render()Step 4: Run and Verify the Pivot Table
- Run the ASP.NET MVC 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 ASP.NET MVC Pivot Table integrated with an ASP.NET Core Web Application to fetch data from a Snowflake database in this GitHub repository.