Free Windows Form Code Generator (WinFormsGen Express) Tutorial

Technologies Used: WinForms, C#, VB.NET, SQL 2012, 2014, .NET Framework 4.5.1

WinFormsGen Express is a free Windows Form (Winforms) code generator software you can download absolutely free and with absolutely no catches. WinFormsGen Express generates WinForms, Code Behind, Middle-Tier code, and Data Tier Code (with empty spots where you can enter your own code) by reading your MS SQL Database. It generates a WinForms application (so you know that all generated code works) in One Click.

Now that I'm done with my marketing spiel let's get on with the tutorial.

You will need the following things for this tutorial:
  • Northwind Database
  • Microsoft SQL Server
  • You will need an Admin username/password or enough rights in the MS SQL Server. A blank username/password will not work.
  • WinFormsGen Express

1. Download WinFormsGen Express here:

2. Install it. Just follow all the prompts. If you need a tutorial go here:

3. Open WinFormsGen Express and enter the following information shown in the snapshots below. Note: Use your database's username and password instead of the one shown below.

Database Settings

Code Settings

4. Click the "Generate Code for All Tables" button. WinFormsGen Express will start generating code and you will see a message once code generation is done.

Generate Code

Done generating code

5. Open Visual Studio 2013/2015, and then open the newly created Windows Form Project.

Open Project

Open Project Dialog

6. The Visual Studio Solution Explorer window shows all the generated WinForms and other objects.

WinForm Project in Solution Explorer

7. Run the project by pressing F5.

Default WinForm, list of generated WinForms

8. Click on one of the links, e.g. the UnboundProducts. You will notice that each field in the Products table are shown here. Foreign keys are also shown as a Combo Box control. There's also validation, for example Product Name here is a required field. Note: WinFormsGen Express generates Unbound (winforms that are not bound to a database) winforms only.

Products Unbound Form

9. What you've seen so far are the generated WinForms and their respective Code Behind. WinFormsGen Express also generated the Middle Tier and Data Tier codes. Close the win form and then go back to Visual Studio. The Middle-Tier (Business Objects) and Data-Tier (Data Layer) code is located in the Code folder. WinFormsGen Express also generated example code for each CRUD (create, retrieve, update, delete) operation it generated. Easily enough you can find all these code in the Example folder under the Code folder. Each table in your database should have a Class file.

Data Layer Folder

10. Since WinFormsGen Express does not generate code that binds the Windows Form to the database, let's bind the WinForm to the database by adding logic to the UnboundProducts Class (.cs) file. Open the Class ProductsExample.cs (.vb) under the Example folder and then copy the code from the Insert method to the Code Behind file Products.aspx.cs (.vb) BtnAddRecord_Click event.

Note: We will use a Layering approach (3-tier, n-tier), so, the WinForm (Presentation Layer/Front End) will access the Products.cs (.vb) (Middle-Tier), and then the Product.cs will access the ProductsDataLayer.cs (Data-tier) code. The Data Layer code would then access our database (MS SQL).

Products Example Class

Product Example Class

Products Example Insert Method. Copy Insert method code shown below.

Product Example Insert Method

Products Unbound UserControl (UnboundAddEditProducts.cs). Here's how this winform looks before copying the insert method code. Note: We're using a usercontrol here because the same usercontrol can be used by the Add or Update Win Form. Copy the Insert method code where it says "place your code here".

place code here

Products Unbound WinForm Code (UnboundAddEditProducts.cs) after copying Insert code from the ProductsExample class

    private void AddOrUpdateProducts()
        // first instantiate a new Products
        Products objProducts = new Products();
        // assign values you want inserted
        objProducts.ProductName = "Chai";
        objProducts.SupplierID = 1;
        objProducts.CategoryID = 1;
        objProducts.QuantityPerUnit = "1020";
        objProducts.UnitPrice = Convert.ToDecimal(18.0000);
        objProducts.UnitsInStock = 39;
        objProducts.UnitsOnOrder = 0;
        objProducts.ReorderLevel = 10;
        objProducts.Discontinued = false;
        // finally, insert a new record
        // the insert method returns the newly created primary key
        int newlyCreatedPrimaryKey = objProducts.Insert();

11. Change the constant values to the respective WinForm Control's values. Note: Supplier ID and Category ID Combo Box have no values, make sure to add items to these controls. The code shown below is just quick and dirty, you will need to check for nulls or empty string before you convert the control's values, but you already know that.

    private void AddOrUpdateProducts()
        // first instantiate a new Products
        Products objProducts = new Products();
        objProducts.ProductName = TxtProductName.Text.ToString();
        objProducts.SupplierID = Convert.ToInt32(CbxSupplierID.SelectedValue.ToString());
        objProducts.CategoryID = Convert.ToInt32(CbxCategoryID.SelectedValue.ToString());
        objProducts.QuantityPerUnit = TxtQuantityPerUnit.Text.ToString();
        objProducts.UnitPrice = Convert.ToDecimal(MtbUnitPrice.Text.Trim().Replace(",""").Replace(" """));
        objProducts.UnitsInStock = Convert.ToInt16(MtbUnitsInStock.Text.Trim().Replace(",""").Replace(" """));
        objProducts.UnitsOnOrder = Convert.ToInt16(MtbUnitsOnOrder.Text.Trim().Replace(",""").Replace(" """));
        objProducts.ReorderLevel = Convert.ToInt16(MtbReorderLevel.Text.Trim().Replace(",""").Replace(" """));
        objProducts.Discontinued = CbxDiscontinued.Checked;
        // finally, insert a new record
        // the insert method returns the newly created primary key
        int newlyCreatedPrimaryKey = objProducts.Insert();

12. The code below accesses the ProductsDataLayerBase.cs (Data Layer) through the Middle Tier object.

BtnSave_Click Event (WinForm/Presentation Tier) Calls the Middle Tier Insert() Method

// finally, insert a new record 
// the insert method returns the newly created primary key 
int newlyCreatedPrimaryKey = objProducts.Insert(); 

ProductBase.cs (Middle-Tier/Business Object) Calls the Data Layer Insert() Method

public int Insert() 
    Products objProducts = (Products)this; 
    return ProductsDataLayer.Insert(objProducts); 

ProductDataLayerBase.cs (Data-Tier/Data Layer)

public static int Insert(Products objProducts) 
    // add your code here 
    throw new NotImplementedException(); 

13. You still need to add code in the ProductDataLayerBase.cs Insert method which will insert your passed values to the database.

Data Layer Code

Assign the Middle Tier values passed by the UI tier to whatever objects you have. Your objects could be an Entity Framework, or whatever you wish to use that will interact with your database. Or you can just buy the WinFormsGen Professional Plus so that you don't even need to worry about the datayer, it should generate this part as well as the respective Stored Procedures (sorry for the marketing spiel).

public static int Insert(Products objProducts) 
    // add your code here instantiate your own object
    // add your code here 
    // assign values to your own object
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.ProductName;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.SupplierID;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.CategoryID;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.QuantityPerUnit;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.UnitPrice;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.UnitsInStock;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.UnitsOnOrder;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.ReorderLevel;
    YourCodeToSaveValueToDbaseHere.Whatever = objProducts.Discontinued;
    // add your code here to insert to database

Last Words:

As programmers we develop for one project until we're done or get pulled to another project. This means using the same database over and over again, adding more tables or fields to existing tables and/or views, or updating them. The "One Click" feature becomes really handy the next time you generate code for the same database, all you have to do is click the "Generate Code for All Tables" button when working on the same database, and that's it. WinFormsGen Express is absolutely free, no need to register, no pop-ups asking you to buy the Professional Plus edition, no marketing/unsolicited emails whatsoever, yup - it's absolutely free.

As always, the code and the article are provided "As Is", there is absolutely no warranties. Use at your own risk.

Happy Coding!!!

Date Created: Saturday, November 14, 2015