Skip to main content Skip to footer

How to Add a New Customer to QuickBooks Online in C# .NET

The QuickBooks Online C1DataConnector provides an easy-to-use component for performing tasks such as adding, updating, and retrieving customer information, vendor information, employee information, transactions, etc. Routines are common in QuickBooks, but what do we do when we need routine logic in our custom application? We can build our own routine that allows us to enable powerful custom workflows through a simple "If this happens…" "then do that" framework. This blog will look at a common task such as adding a Customer and show how to automate that process in a Blazor Server application with C#.

This tutorial also works as an easy walk-through using the QuickBooks Online (free) sandbox account to get started using C1DataConnectors.

Download Now!

Connecting to QuickBooks Online

To connect to QuickBooks Online, use your existing account, or create a new developer account. Then create a new app named "MyC1App". Select the accounting scope since we are just working with the free playground.

image1

From the online dashboard, obtain the following for your app:

  • Client ID
  • Client Secret

Next, download, build and run this ASP.NET MVC5 playground sample. This will help walk you through generating the OAuth tokens to access the QuickBooks API. Follow each step the sample provides and take note of the following credentials:

  • Realm ID (use the Realm ID as the Company ID in your connection string)
  • Access Token
  • Refresh Token

Now you have all the keys you need to access your QuickBook data source from any .NET app.

Building the Blazor Server App

For this sample, we will build a web form that accepts a prospective customer's email. Then we will automatically add that customer to our QuickBooks Online storage.

For example, if a customer submits their email through a web app form, we can automatically add that customer to QuickBooks Online.

Add ComponentOne DataConnectors Library

Create a new Blazor Server application and add the C1.AdoNet.QuickBooksOnline package to your project.

image2

Build a simple form using some basic InputText and a button so that we can collect user input such as their name and email.

image3

Inserting a Customer into QuickBooks

In addition to querying, the C1DataConnectors support other CRUD operations such as inserting, updating and deleting data. The data connector supports these operations using Command and DataAdapter objects. The following C# code shows how to insert a new customer into the Customers table using the QuickBooks Online sandbox account. Fill in the connection string x's with the values you obtained earlier.

C# Code:

@using C1.AdoNet.QuickBooksOnline
@using System.Data
...
<button @onclick="SubmitCustomer">Submit</button>
...
@code{

    //Configure Connection string
    C1QuickBooksOnlineConnectionStringBuilder builder = new C1QuickBooksOnlineConnectionStringBuilder
    {
        OAuthClientId = "x",
        OAuthClientSecret = "x",
        OAuthTokenEndpoint = "https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl",
        OAuthAccessToken = "x",
        OAuthRefreshToken = "x",
        CompanyId = "x", //Use your Realm Id
        UseSandbox = true
    };

    public void SubmitCustomer()
    {
        using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(builder.ConnectionString))
        {
                //Populate DataTable
                C1QuickBooksOnlineDataAdapter adapter = new C1QuickBooksOnlineDataAdapter(conn, "Select * from Customers");
                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);

                //Create Insert command
                adapter.InsertCommand = new C1QuickBooksOnlineCommand(conn);
                adapter.InsertCommand.CommandText = "Insert into Customers(GivenName, PrimaryEmailAddr_Address) Values(@GivenName, @PrimaryEmailAddr_Address)";
                adapter.InsertCommand.Parameters.Add("@GivenName", "GivenName");
                adapter.InsertCommand.Parameters.Add("@PrimaryEmailAddr_Address", "PrimaryEmailAddr_Address");

                adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;

                //Insert new row
                DataRow customerRow = dataTable.NewRow();
                customerRow["GivenName"] = inputText1.Value;
                customerRow["PrimaryEmailAddr_Address"] = inputText2.Value;
                dataTable.Rows.Add(customerRow);

                //Update database
                adapter.Update(dataTable);

        }
    }
}

And that's it! If you load the Customers into a datagrid, you can see the added customer. You can check out the online documentation for additional query examples.

image4

Why Use ComponentOne DataConnectors?

You may have stopped and asked yourself - why should you use C1DataConnectors in this instance rather than the QuickBooks .NET SDK? The main benefit is that you can easily connect to a variety of data sources using one library. So now you can easily add customers to Salesforce, Dynamics, etc, without having to learn a completely different API. C1DataConnectors also adds value to an application already using ComponentOne controls if they ever need to integrate data from some different CRMs.

Download Now!


comments powered by Disqus