Tutorial: MSSQL Add Database Google Employee Form [ Part 1]
Creating The Database, The MenuStrip Framework, DisplayMajors, & DisplayEmployees

Dr. Thomas E. Hicks
Computer Science Department
Trinity University

 


ABOUT

1] This tutorial was designed to show how to create a basic windows form which will enable the user to update a Microsoft SQL Server database.

2] User skills will vary; for the sake of completeness, I have created a number of support tutorials that have been created to illustrate and teach various aspects of the Visual Studio .NET application environment and SQL Server 2008 along the way. These tutorial are all hyper-linked in red and appear just before you are likely to need them. Good Luck!


Components

Tutorial: MSSQL Database Google Employee Form [ Part 1]
Creating The Database, The MenuStrip Framework, DisplayMajors, & DisplayEmployees
 

Tutorial: MSSQL Database Google Employee Form [ Part 2]
Add Employee, Edit Employee, Delete Employee

 


Software Requirements

Either Visual Studio 2005 or Visual Studio 2008
I am using Visual Studio 2008 Professional

Tutorial: Visual Studio 2008 Install

Tutorial: Visual Studio 2008 SP1 Install

Any Version Of Microsoft SQL Server Or SQL Server Express
I am using the free Microsoft SQL Server Advanced

Tutorial: Microsoft SQL Server Express- 2008 Advanced (With Server Management Studio) Express Install

 


Create A Simple Database

Tutorial: MSSQL Database Construction With SQL Server Management Studio

1] Create a database, called Google. Within Google, create a table, called Employees, that has

2] Within Google, create a table, called Majors, that has

3] Google Employees have a number of different majors. For purposes of this tutorial, we will need a little data. Add the following to your Majors table:

4] Google Employees are funny people that live all over the country.  Add the following to your Employees table:

 

 


Download Database

Tutorial: Import-Export-Backup Of MSSQL Database With Microsoft SQL Server Manager

1] I have included the database files for those of you who know how to use Microsoft SQL Management Studio Express to import the database, described above,  into Microsoft SQL Server

Google-mssql.zip  Google.bak

2]  I have included the database files for those of you who know how to import a Microsoft Access database into Microsoft SQL Server

Google-Access.zip  google.mdb


Create A Windows Application

Tutorial: Visual Studio Windows Application - Forms - Basic Forms

1] Create a Windows Application, called Google.

2] Name the opening form MainMenu.

3] Include a Text commercial "Google Application - Main Menu - Written By ...."


 


Transform The MainMenu Form Into A MenuStrip MDI Container

 Tutorial : Visual Studio Windows Application - Forms - MenuStrip Driven Application

1] Change IsMdIContainer to true.

2] Add a MenuStrip Object

3] Apply your desired Font and Color Scheme to the MenuStrip.

4] Add the following choices to the MenuStrip

 

5] Name each of the Menu Strip choices. You might want to use the same names as I have selected.

 


Download Google1

1] Just in case you have had problems, I have zipped the Google Application above and made it available for download on our Server.

Google1.zip 


MenuExit

Tutorial: Visual Studio Windows Application - Forms - Menu Driven Application With Multiple Forms

1] Double-Click on the Exit button (on form MainMenu) and enter the code to exit the Google Application.

2] Test the button to make sure that it works.


Create Form DisplayMajors - Link The Control To The MainMenu Form

Tutorial: Visual Studio Windows Application - Forms - Basic Forms

Tutorial: Visual Studio Windows Application - Forms - Buttons & Color Picker

Tutorial : Visual Studio Windows Application - Forms - MenuStrip Driven Application

1] Create another C# Windows Application Form

2] Name the form DisplayMajors.

3] Apply your desired Font and Color Scheme to the DisplayMajors Form.

4] Include a Text commercial "Google Application - Display Majors - Written By ...."

5] We would like the MenuStrip to control all form opening and closing for the project. The following block of code disables the close button on our form. Add it to the DisplayMajors form.

        private const int CS_NOCLOSE = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
                return cp;
            }
        }

6] Add a button, named btnClose, to the DisplayMajors form.

7] Enable the click event of button btnClose to hide the DisplayMajors form.

8] Return to the MainMenu form. Add the code to disable the close box to the MainMenu form.

9] We would like the MainMenu to create, hide, and show all forms in the Google Application.

10] Create a DisplayMajors object, called displayMajors, at the top of the MainMenu class; this is the first step toward making this form available to other forms and functions in our project.

 

11] Creating the form object does not make it visible.  Return to the MainMenu form. Double-Click on Display Majors to create a click event for the MenuStrip button. Add the code to display form displayMajors.

12] Associate this DisplayMajors form with the MDI container MainMenu. This association should be done in the MainMenu initialization. This binds the our Display Majors form to our MainMenu parent container.

13] Run the application. Note that the close button is disabled on both forms. Note that the DisplayMajors form may be opened and closed repeatedly. Note that the DisplayMajors form stays within the confines of the MainMenu window. Note that you may change the size of the MainMenu window. Note that the DisplayMajors form can be minimized; it drops to the bottom of the MainMenu window.

 


Download Google2

1] Just in case you have had problems, I have zipped the Google Application above and made it available for download on our Server.

Google2.zip 


SQL Server Service & Instance

1] If you installed SQL Server, you were provided with an opportunity to name the SQL Server Instance. The default was sqlexpress, but you may changed it to something else. If it were installed with Visual Studio, the name would server instance would also be named sqlexpress. Perhaps a system administrator installed the application. You can see that I chose to use SQL in the installation below!

2] All efforts to connect to the database will fail if the SQL Server is not running. There are many ways to check and see if the SQL Server is running.

3] Open the Control Panel. Open Administrative Tools. Double-Click on  Services. See Below!

3] Open the Control Panel. Open Administrative Tools. Double-Click on  Services. See Below! There are two SQL Services running on the Windows 2008 Server shown below.

4] There are also two SQL Services running on the Windows 2008 Server shown below.

5] The Service should be both started and have Automatic startup type.

6] For purposes of this tutorial, I shall use instance SQL

7] You can start and stop the service by logging into Microsoft SQL Management Studio Express. The instance on computer Laptop is SQL.

8] Throughout this tutorial, you might want to log in to Microsoft SQL Management Studio Express to monitor the successes, or failures, of our application.



 


Create A Database Connection To The SQL Server

Tutorial: Visual Studio Windows Application - Forms - MSSQL GUI Connections & DataGrids

1] Open the DisplayMajors Form

2] Open the Server Explorer. Using the mouse, hold down the view menu and select Server Explorer. The Server Explorer offers one more way to view the services.

3] Using the mouse, right mouse click on the Data Connections and select Add Connection... (See Below!)
 

4] Select Microsoft SQL Server. Using the mouse, push/select the Continue button. 

5] Either enter your server name (instance LAPTOP\SQL) or use the drop-down to select it.

6] If you plan to run the database application on the computer running the database, you may select to use Windows Authentication. If you plan to run it on another computer, you will need to configure an account and establish the necessary permissions and open any firewalls; this is beyond the scope of this tutorial. Use Windows Authentication for now; you can come back an change it later.

7] Either enter the database name (Google) or use the drop-down to select it. Push the Test Connection Button!

8] If the connection does not succeed, you have some type of SQL Server problem; good luck!  Push the OK Button!

9] Note the new connection in the Server Explorer.

 
Add A DataGridView To The DisplayMajors

Tutorial: Visual Studio Windows Application - Forms - MSSQL GUI Connections & DataGrids

1] Drag a DataGridView, from the toolbox to the DisplayMajors form. 

2] Open the DataGridView control.  Hold down the Drop-down and select Add Project Data Source. (See Below!)

3] Using the mouse, select the Database. Using the mouse, push/select the Next button.   (See Below!)

4] This selection is simple since we have created only one connection. We could create a new one if needed. Using the mouse, select/push the Next button. (See Below!)

5] Save the connection string. You might want to use it with other forms and applications.  Using the mouse, select/push the Next button. (See Below!)

6] We want the Majors.ID and Majors.Major in our grid. Select the data. Using the mouse, select/push the Next button. (See Below!)

7] Note that you can see the DataSet, BindingSource, and TableAdapter, created in this process, at the bottom of the form.

8] When I first run the form, it appears not to work. But there is a 1 in the first ID? 

9] If on the other hand, I drag my mouse over the form, Majors data will seem to appear. Problem: I selected white text on the Green Background as the default for my form. The white text does not show up with the default white background in the grid. (See Below!) 

10] Open the DataGridView control.  Hold down the Drop-down and select Add Project Data Source. (See Below!) 

11] By default, the database table field names appear at the top of the columns. You can change them. Change the Header Text from ID To Major ID. Push the OK button and test it.
 

12] Open the DataGridView control.  Hold down the Drop-down and select Dock in parent container. (See Below!) 
 

13] The data completely fills the window. Perhaps we don't want to do it. Undock the form.

14] Open the DataGridView control. Return To Edit The columns.

15] It is time to edit the DefaultCellStyle

16] We have opened the cell style for the first column. change the ForeColor, SelectionBackColor, and SelectionForeColor to combinations that work for you. Do the same for Major column.

17] We are starting to look better, but the major field is not wide enough. 

18] We can just drag it wider, but it closes back when we stop the application. 

23] Numeric data should be right justified. Note that I have aligned ID on the MiddleRight below. 

24] Character String data should be left justified. Note that I have aligned ID on the MiddleLeft below.
 


25] In an effort to lock the columns, let us dedicate 100 pixels to the ID column.

26] In an effort to lock the columns, let us dedicate 300 pixels to the Major column.

27]  Run the application. Better, but it would be nice to have the data in alphabetical order by Major.

28]  There are many more things you can do with grids. You can set them up to edit and delete data, but that is beyond the scope of this tutorial. A lot of my database design involves locking records in a multi-user mode; the grids have no support for concurrency.

29] I normally create a number of queries and views with SQL Server Management Studio and link my grids to those, but the grid query can be changed.

30] Since my students, and some consultants that I know, delete the DataGridView and reconfigure it each time they wish to change it, I will show you how to alter the query before leaving this form. Open the DataSet control. and Select Edit In DataSet Designer


31] Fill, GetData() is contains the query used to fill the grid. Using the mouse, touch Fill, GetData() (on  the right below!)

32] Open the CommandText.

33] There are lots of ways to alter the query. Knowing a little SQL makes it easy.

34] Our query will now display the ID and Major in order by Major.

35] Yes update the application. Push Yes.

36] Yes update the application. Push Yes again.

37] Run the application. All of the work with fonts, colors, sizes, and alignment need not be redone when you alter the query in this way.


38] There is still one issue with using the GUI form that will require some attention. We will address this in Part 2 of the tutorial.

 


Download Google3

1] Just in case you have had problems, I have zipped the Google Application above and made it available for download on our Server.

Google3.zip 


Create Form DisplayEmployees

1] Create Form DisplayEmployees. Create object displayEmployees and associate it with the MainMenu form in the same way that we did DisplayMajors. Make the form look something like the following:

 


Download Google4

1] Just in case you have had problems, I have zipped the Google Application above and made it available for download on our Server.

Google4.zip