Before starting this project, be sure you have met the Prerequisites listed in Compiling Your Projects. Perform the following steps to create a simple database application using the tables created in the previous exercise.
Start the Visual Studio IDE.
From the File > New > Project menu, create a new Visual C# Windows Application (or "Windows Forms Application" in later versions of Visual Studio). You may name it whatever you choose.
If your copy of Visual Studio does not have templates for those project types, open Visual Studio’s New Project dialog and click the link labeled Not finding what you are looking for? Open Visual Studio Installer. Then add the .NET desktop development workload, as discussed in the Prerequisites section of Compiling Your Projects.
Note: After the new project is created, check the target framework under Project > Properties > Application to ensure that “Target Framework” is not a "Client Profile." For example, if you see ".NET Framework 4 Client Profile", change this setting to ".NET Framework 4" (or 4.5 for VS 2019). The Client Profile does not contain the necessary Data namespace for this tutorial.
With your project (and not the solution) selected in the Solution Explorer, do the following: From the Visual Studio's main menu, select Data > Show Data Sources as shown below (or, in later versions of Visual Studio, select Project > Add New Data Source..."):
The Data Sources explorer should now be displayed. Click the Add New Data Source option. Note that this step is only necessary for older versions of Visual Studio. If your version of Visual Studio takes you directly to the “Choose a Data Source Type” window (shown in the following step), you can skip this step.
Choose Database and click Next:
In newer versions of Visual Studio, a “Choose a Database Model” dialog box will appear where you select Dataset and click Next.
Click the New Connection button:
You will be prompted with a list of Data Sources. Select the FairCom data source and Click Continue or OK.
Note, if an intermediate screen is displayed with a prior data source, click the Change button.
If the FairCom item is missing from the list, it means the c-tree ADO.NET driver has not been installed. Close Visual Studio and install the driver using the steps described in the Prerequisites section of Compiling Your Projects.
Note: If clicking the New Connection button causes the "Data Source Configuration Wizard" to disappear, it is likely that you have an old version of the ADO.NET driver (Ctree.Data.SqlClient.dll) in the Windows Global Assembly Cache (GAC). To correct this problem, perform the following steps:
Save your project and exit Visual Studio.
Shut down the c-tree server (see Setting up the FairCom Database Engine on Windows).
Use FairComConfig.exe to remove the ADO.NET driver (see Setting up the FairCom Database Engine on Windows).
Open a Developer Command Prompt window. It is important that it is opened “as administrator” for this procedure.
Type the following command to see if a copy of the DLL is still in the GAC (the first parameter is a lower-case 'L'): gacutil /l Ctree.Data.SqlClient
If one is listed, use this command to remove it: gacutil /u Ctree.Data.SqlClient
If this fails, reboot your computer and return to step d of this procedure.
If this still fails, contact FairCom.
Once Ctree.Data.SqlClient has been removed from the GAC, you can re-install the ADO.NET driver, start the c-tree server, launch Visual Studio, load the project you saved earlier in step 1 and return to step 4 (earlier in this section, where you are using Data > Show Data Sources).
The "Add Connection" dialog is displayed, with all required connection parameters already filled in such as IP Address, SQL Database Name, User Name, and Password. You can leave these values as the defaults. Click the Test Connection control. You should see the "Test connection succeeded" dialog display.
If you do not get a successful connection, it is possible your installation was not completed properly. The FairCom Database Engine is installed and started as a Windows service as a default. Check the Windows services manager, or, alternatively, re-install FairCom DB.
If you are using a developer's license, it is also possible that the FairCom Database Engine has timed out and needs to be restarted. See Monitoring the FairCom Database Engine.
Click OK from the Test connection succeeded message, and then OK again from the "Add Connection" dialog.
You should now be at the following screen. Click Next.
You are presented with the following screen. Leave the settings as they are, and click Next. Note that it is OK if the connection string has a different name, like “ConnectionString”, for example.
Expand the Tables node, select the "custmast" table (giving it a check mark, as shown in the following image), and then click Finish.
If the tables shown below are not listed, it means you did not populate the sample database, or you ran one of the console-based tutorials, which deleted the sample database. Refer to the prerequisites section in Compiling Your Projects to reload the sample database. You can refresh the following screen by hitting the Back button and then the Next button. It is OK if the “DataSet name” at the bottom of the window is different from the following picture.
The Data Source Configuration Wizard adds the new data source to the Data Sources window.
Now that we have the FairCom DB Data Source established we can preview data from the Data Sources window. Right-click the "custmast" Table and select the Preview Data option from the pop-up menu:
Click the Preview button on the next screen, and the records/rows in the "custmast" file/table will be displayed.
Let us now return to the form design mode (by clicking the Close button on the "Preview Data" window) and add several Data-Bound Controls to the form. From the Data Sources explorer, drag each field from the "custmast" table onto the form, positioning the controls as you like.
Note: When you drag the first field to the form, Visual Studio adds a Binding Navigator control.
If you want, you can use the Properties Inspector to change the names of the fields you just created to something more descriptive. Here is what our project looks like after we renamed our fields:
Now save and build your project. Run the project by pressing the F5 key. Experiment with the navigation controls along the top of the window to view the test data.
Using this program you just built:
Keep in mind that this program is just a bare-bones “proof of concept” test program, and not a fleshed-out application. That means, among other things, that no exception handling has been put in place yet; so, this program is very easy to crash with unhandled exceptions. To reduce the chance of that happening, here are some things to keep in mind while using the program:
The “Customer Number” field (cm_custnumb) is the primary key. Leaving it blank when adding records will cause an exception. Also, attempts to repeat an existing Customer Number when adding a new record will cause an exception. Primary keys must be unique.
Click the yellow “+” button to add a record. Once you are done typing in the new values, click the Save button (shaped like a floppy disk) to save your work to the c-tree database.
To delete an existing record, use the arrows to navigate to the record of interest. Then click the Delete button (shaped like a red “X”). The record immediately vanishes from the program. Note that the record does not get deleted from the c-tree database until you press the Save button. Please remember to press the Save button to keep this program and the database from getting out of sync.
As you have experienced, it is easy to build a simple Data-Bound application. As ADO.NET is an industry standard there are numerous publications available describing ADO.NET programming in depth. Any of these are appropriate to begin using the FairCom Database Engine in your applications today!
The next chapter presents an alternate technique in the Microsoft Visual Studio environment. It illustrates creating a ToolBox Tab and adding the FairCom DB ADO.NET Framework component to make it accessible for all projects. You can then use these components in your own data grid programs.