Salesforce ADO.NET Developer Guide - Winform App

This article provides a guide for creating a Windows Forms App in Microsoft Visual Studio 2022. With the help of the CData ADO.NET Provider for Salesforce, this application will create a form that loads and displays data from a selected Salesforce table.

NOTE: While this article refers to Salesforce data, the principles can be applied to any of the 270+ data sources CData supports.

Prerequisites

This guide requires the following parameters:

Guide

  1. Create the application
    1. Open Visual Studio and create a new “Windows Forms App (.NET Framework)” project.
    2. Name your project “WindowsFormsAppSalesforce”, so that the namespaces will match if you copy and paste code from this walkthrough, and then select “Create”.
  2. Add a reference to the required assembly
    1. Add a reference to System.Data.CData.Salesforce.dll, located in the lib subfolder in the installation directory (ex. " C:\Program Files\CData\CData ADO.NET Provider for Salesforce 2024\lib").
    2. Build the project to complete the setup.
  3. Rename and add controls to the form
    1. Right-click on Form1.cs in the Solution Explorer, select Rename, and change it to ConnectForm.cs.

    2. Open the Form Design and from the “Toolbox” on the left of the Server Explorer, add a DataGridView, TextBox, and a Button to your form.

    3. Adjust the Name properties for the controls added:

      • Select the TextBox and set the Name property to “txtTables”
      • Select the Button and set the Name property to "btnConnect"
      • Select the DataGridView and set the Name property to "dataGridView"
  4. Add code to ConnectForm.cs
    1. Open the form's code by right-clicking on the file and choosing “View Code”.
    2. Make sure to add and reference "System.Data.CData.Salesforce" at the top of the namespace to connect to Salesforce data: using System.Data.CData.Salesforce; ...
    3. The first code snippet added is a "ConnectForm" class constructor which uses "this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);" to dynamically associate the click event of the btnConnect button with the btnConnect_Click event handler. This means that whenever the button is clicked, the btnConnect_Click method will execute. Additionally, update the ConnectForm text to "Connect" to match the form: public ConnectForm() { InitializeComponent(); // Subscribe to the Click event of btnConnect here this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click); this.Text = "Connect"; }
    4. The second code snippet defines the event handler "btnConnect_Click". This code gathers the Table name input in the TextBox and then sets the connection string to connect to that table. The code then uses the connection string to load and display data from the selected Salesforce table in the Data Grid View: private void btnConnect_Click(object sender, EventArgs e) { string selecttable = txtTables.Text; string connectionString = $"AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;Tables={selecttable}"; try { using (SalesforceConnection connection = new SalesforceConnection(connectionString)) { SalesforceCommand command = new SalesforceCommand($"SELECT * FROM [{selecttable}]", connection); SalesforceDataAdapter adapter = new SalesforceDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); dataGridView.DataSource = table; } } catch (Exception ex) { MessageBox.Show($"Error loading data: {ex.Message}"); } }
  5. Run the application

    With everything set up, you can now run the application that loads and displays data from the selected Salesforce table. To do this, first, build the solution then use the green play button to start the application. This should open the application, and you can enter a table name to display data, as shown below:

Unlock seamless data connectivity in your .NET applications

Are you looking to unlock the full potential of your Salesforce data? CData's Salesforce ADO.NET Provider can seamlessly integrate your .NET applications with your Salesforce data. With the CData ADO.NET Provider for Salesforce Community License, you get free-forever libraries to access your data in personal .NET projects, all through familiar SQL. Request a license and start building Salesforce-connected apps today!