4.13.2 Usage: Creating A New Windows Forms Application

To begin creating a new Windows Forms Project, you can either:

From the MySQL toolbar, click the New Project button to open the Create new MySQL project wizard.

Figure 4.44 Creating a New Windows Form Project from the MySQL Toolbar

Creating a New Windows Form Project from the MySQL Toolbar

Figure 4.45 Creating a New Windows Form Project from the Project Selector

Creating a New Windows Form Project from the Project Selector

Step one -- Select the database connection for web authentication:

First, set up a connection to your MySQL Database.

Figure 4.46 Windows Forms Project: Data Source Configuration

Windows Forms Project: Data Source Configuration

The data source configuration step allows you to customize the connection and database for your application. The connection string can be selected from the dropdown list, or a new connection can be set up by clicking New Connection .

After the MySQL connection is set up and selected, the connection string is listed for review. While the wizard can generate a MySQL connection string that includes your credentials, this insecure practice is not recommended because the credentials would be shown as plain text in the web configuration file.

In addition to the data connection, you must also define the data access technology used to connect your database with the forms that the wizard will generate. You can choose between three options:

Step two -- Database objects selection:

Now, a list of tables from your selected database is listed. Select the tables used by your application.

Every table represents a Form in your application. In the grid, the Customize column contains buttons that trigger their own wizard that helps you select the layout for the generated form for the individual tables.

Figure 4.47 Windows Forms Project: Database objects selection

Windows Forms Project: Database objects selection

There are three types of views (layouts) that you can select from when customizing the forms:

By default, selected tables are configured using the Single Column layout.

For any type of generated model, it includes all of the tables for the required CRUD, including extra tables for master, and referenced tables in foreign keys.

Figure 4.48 Windows Forms Project: Database objects selection, continued

Windows Forms Project: Database objects selection, continued

Clicking Customize to customize the layout or view for a table. Only one type of view can be selected for one table.

Additionally, the wizard can add customized validations based on each Column data type. To add these validation procedures, check the Show all the advanced steps in the wizard option. This adds an additional validation step in the wizard.

Figure 4.49 Windows Forms Project: Customizing database object layout / views

Windows Forms Project: Customizing database object layout / views

The following Columns Validation step is used to configure the validation options.

Figure 4.50 Windows Forms Project: Columns validation

Windows Forms Project: Columns validation

Setting up the columns validations options

By default, the generated CRUD already enforces data type validations (such as not accepting letters when the type is numeric, only accepting well formed floating point types, checking boolean types (through check boxes), accepting valid dates, and so on.). Additionally, this step permits you to configure extra validations, like setting Maximum/Minimum values, enforcing foreign key integrity when the 'HasLookup' option is checked for foreign key columns, and so on.

The information on column datatypes and required status is automatically deduced from the database, and is read-only.

The validations are implemented using the Windows Forms ErrorProvider control, and the Validating event for the distinct controls.

Note

The column validation step can be repeated if the Master-Detail is chosen. In our example, the second validation screen will be for the columns of the Detail table.

The following are a series of screenshots (with explanations) that relate to the validation routines and options.

Single-Column layout

The toolbar appears in all of the layouts, and the buttons are defined as (from left to right):

This screenshot shows the Customer table in the sakila database.

Figure 4.51 Windows Forms Project: Example Form

Windows Forms Project: Example Form

The following is an example that demonstrates ErrorProvider complaining that the FirstName column is mandatory (cannot be empty).

Figure 4.52 Windows Forms Project: Example Form, with Error

Windows Forms Project: Example Form, with Error

Another example for the generated CRUD using a DataGrid layout that also shows the datetimepicker control displayed while editing a date type column.

Figure 4.53 Windows Forms Project: Example Form, DataGrid with DateTime Picker

Windows Forms Project: Example Form, DataGrid with DateTime Picker

Several of the buttons are disabled in the DataGrid layout. In this case, use the standard shortcuts of the Windows Forms DataGridView to manipulate records. For example:

  1. Go to last row (it is blank) in the grid to insert a new record

  2. Select the current row as a whole using the grey rows on the left, and click Delete to remove the record

  3. You still need to commit the changes when using the Grid layout

This last example shows a Master-Detail relationship using the film and film_actor tables in the sakila database.

Note

Notice that friendly columns names were chosen by the wizard for the lookup combo boxes that correspond to the foreign key's language (in film table), actor_id, and film_id (the last two for the film_actor table).

Figure 4.54 Windows Forms Project: Example Form, Master Detail

Windows Forms Project: Example Form, Master Detail