By Adrian Sobers
Introduction
The purpose of this article is to re-visit some
of the issues which were experienced when implementing a simple C# application -
Scrabble Records. The Scrabble Records application is a C# application which
keeps a running record of scrabble game scores (and ranks players accordingly -
still to be implemented!). I hope to cover
some of the areas in C# which developers, new and experienced alike, will come
across. Seeing as C# is relatively new, I was tired reading about it and going
through the sample walkthroughs in Visual Studio.NET 2002. Walk with me through
the full process of developing the Scrabble Records Application.
Implementation Tools
The development platform used will be
Microsoft.NET Framework v1.0.3705, the IDE is Visual Studio.NET 2002 (a must I
think for working with .NET) and of course the language of choice is C#...Visual
Basic dot who?
Designing the
Application
Before jumping in to write the code, we have to
first of all decide what on earth the application is supposed to do. Doesn't
matter if the language has 1million great new features and will make us more
efficient programmers, we still have to plan. So as an overview, big picture if
you will, the application will be a single user Microsoft Windows application
which will allow the user to enter scores for one or more scrabble games. The
information entered will be stored in a database (Microsoft Access...don't you
just love MS!). The user will then be able to view the scrabble records entered.
Now that we have a clear mental picture of what we want to create, we can start
coding. Oh, to make it a bit more exciting so you don't fall asleep, the code
name for the application is TongBei. TongBei is a martial arts fighting style
(used by Frost for MK5 fans).
Enter Visual
Studio.NET 2002
For developing Windows based applications using
C# and the .NET framework, Visual Studio.NET will be your best friend for sure.
We will first develop our main application window. In the project it is the file
MainWindow.cs, this is windows form will contain the application menu which
handles the main events of the application. The menu will allow the user to
enter player names, then scrabble game records. The menu structure is shown in
the figure below:

Figure 1 : Menu
Structure
The MainWindow.cs has some interesting things
that we should note. First of all, this Windows Form is special because it
serves as a container for all other Windows Forms in the application. We must
therefore set the IsMdiContainer property
to True. This lets the compiler know that
this form is a Multiple Document Interface Application (MDI Application) which
will contain the child-windows with which the user will interact with in the
application. Also since MainWindow.cs is the parent-window for the application,
it is useful to set it's WindowState
property to Maximized. This property
determines the initial visual state of the form. It will be easier to manipulate
child-windows having maximized the parent window, so this is the reason we set
WindowState to Maximized.
The menu was created using the MainMenu control
from the Windows Forms toolbox in Visual Studio.NET. Simply drag this control
onto the form you wish to have the menu presented to the user and type in the
menu options which will be available in the application. The figure below
illustrates creating the Scrabble Records menu:

Figure 2:
Creating Menu Structure
Handling
Child-Windows
Ok so how do we handle the child-windows in the
application? How do we 'call' the child-windows into action when they are
requested by the user? Each option on the menu in the application is associated
with a click event-handler. When the user selects Enter Player Record from the
menu, the event-handler for that child window is invoked. It is within this
event-handler that we code what events we want to occur when a particular menu
item is clicked. For example the Enter Player Record menu option has the
following even-handler:

Figure 3: Enter
Player Record menu item event-handler
The code is pretty much self-documenting and
explains each line of the event-handler. This is also a must, commenting your
code is a must. It reminds you what the heck you were trying to do when you come
back later on in the application life-cycle. It's also handy for other people to
learn from when they are reading your code. So when we select Enter Player
Records from the menu the event handler in Figure 3
(above) is called and we create a new instance of EnterPlayerRecords.cs which is
a child-form in our application, we then set MainWindow.cs to be the parent of
EnterPlayerRecords.cs and once that is set up, we display the child-form to the
user and that ends the event-handler.
Each menu-item is associated with a click
event-handler, which calls the related child-form. Looking back to Figure
3 the child-window EnterPlayerRecords.cs was
invoked once the user selected Enter Player Records from the menu in the parent
window MainWindow.cs. In Scrabble Records, each menu option is associated with a
task the user needs to perform, in this case it is Entering Player Records
(names).
First Look Data
Storage Strategy: Entering Player Records
The application essentially will have to store
information which will be retrieved (it's only reason we store information
anyways!) later. When using the .NET framework we have several options to store,
retrieve and manipulate data. For Scrabble Records, I chose to use OleDb.NET as
the data provider. The driver used to accomplish this is Microsoft.Jet.OleDb
4.0.
When the user selects Enter Player Record from
the main menu, they will be presented with the following Windows Form, which is
EnterPlayerRecords.cs in our application:

Figure 4: Enter
Player Record(s)
The user would then Enter the Full Player Name
and click OK, this information would then be written to the database. We'll
examine how that is accomplished.
Visual Studio.NET provides some wizards which
make connecting to data-sources and retrieving information a cinch. However the
code generated by these Wizards can lead to trouble down the line and you can
waste precious time trying to get drag-and-drop connections to work. The first
time I wrote this application I used the Connection Wizards in Visual Studio.NET
and if you needed to change something later on, it can be a hassle, so in this
version, I coded the connections and retrieval from scratch and it wasn't as bad
as I thought it would be. So as a general rule for C# and .NET development I'd
say forget the wizards in Visual Studio.NET and code from scratch, when dealing
with connecting to data-sources and retrieving data.
Connection Strings,
Connection and Command Objects...
The figure below shows what we need to set up the
connection to our database and insert the player names once entered:

Figure 5: Database
connection and query information
We need to declare a variable to store our
connection string, an OleDbConnection object as well as an OleDbCommand and a
string to hold the query to insert player names. We then attempt to establish a
connection to our database and catch any errors that might occur. This code is
placed in the form's load method. When the form is loaded into memory, the code
establishing the connection is executed. This is in the figure below:

Figure 6: Connecting
to Database
We create a new OleDbConnection based on the
connection string we defined in Figure 6
then open the connection by calling the Open() method on the connection object -
simple. This is of course placed in a try/catch/finally control structure so as
to handle any errors that might occur from attempting to connect to the
datasource. And as usual comments are placed before the method to let us know
what the method is supposed to do and it's good programming practice - whether
coding for home or Fortune 500 enterprise - and especially if coding from home
FOR a Fortune 500 enterprise.
Once we've set up the connection to the database
and the user has entered a player name, we need to insert that data into the
database. For the names, this is simple again. We have a table in our database
called tblPlayer into which we will insert
the name entered once the user clicks OK on the Enter Player Record(s) form.
To insert data into the table, this is one of
several ways that can be used. Let's examine the following code:

Figure 7: Inserting
user data to database
The code-snippet in Figure 7
is the beginning of the event-handler for when
the user clicks the OK button to enter a player name. First we check that the
textBox is not blank so we know that we are dealing with actual data. Next we
set the queryString to an SQL statement which inserts the player name in the
textBox into the database field [Name] in the table [tblPlayer]. To perform the
actual insert we create a new OleDbCommand object based on the query and
connection - then we call the ExecuteNonQuery() method which executes a SQL
statement against a connection.
Now we can look at the rest of the method in the
figure below:

Figure 8: Remainder of
event-handler for Button OK click event
The remainder of the method pretty much catches
and deals with any exceptions that may occur during input by displaying the
appropriate message to the user via the MessageBox.Show() method. In the finally
block we clear the textbox control so that the user can enter as much names as
they wish.
One note before we move on about closing
connections. We need to explicitly close the connection to the datasource once
we are finished with it. This is done so that we release any resources which we
do not need. This is placed in the form's Dispose() method, see the figure
below:

Figure 9: Cleaning up
resources
Moving on...Entering
Game Scores: OleDbDataAdapters, DataSets...
When the user selects Enter Game Scores from the
main menu, they are presented with the following form:

Figure 10: Enter
Game Scores
We will examine some of the more important
features of this windows form. From Figure 10
we can see that the Player 1 Name Combo Box is
populated with names from the database. The code to accomplish this is shown
below:

Figure 11: Binding
player names from database table to Combo Box
First we do the usual connecting to the database
and opening the connection and storing the SQL statement as a string. Next we
create DataSet and OleDbDataAdapter objects. The DataSet is an in-memory
representation of DataTable objects, and allows us to manage these tables and
relationships between these tables. The DataAdapter serves as the link between
the datasource and the Connection object with the DataSet object through the use
of its SELECT and action query Commands.
We create an OleDbDataAdapter based on our
namesQueryString and our connection object (to the database). We then call the
fill method of the DataAdapter object using the DataSet object created and the
DataTable name of "PlayerOneNames". Next we set the DataSource property of the
PlayerOneName combo box to the DataTable - PlayerOneNames, then we set the
DisplayMember property of the PlayerOneName comb box to show the name field from
the database, which stores the player names - now the player names from the
table in the database are displayed in the combo box on the Windows form.
The input of game data, player 1 and 2 name along
with their respective scores and the date the game was played is handled in the
following code-snippet:

Figure 12: Entering
Game Data
The same technique used in entering the player
name to the database is used here. The concept is the same, the SELECT statement
is just longer that's all because we are dealing with five fields instead of
one. The other thing to note is that we have to provide some sort of feedback to
let the user know that the record was input successfully to the database, that
is done via the MessageBox.Show() method, which includes the string feedback,
which is just stores the text equivalents of what was input onto the form. There
is nothing else really to this form...time to move on.
Displaying our Data
When the user selects View Player Records from
the menu they are presented with a Windows form, which holds a DataGrid that
displays the results of the games to date. Here is what it looks like:

Figure 13: Viewing Game
Records
We will now look at the main part of the method
that handles displaying the Game Records data on the Windows form:

Figure 14: Displaying
Data via DataGrid
Once we have connected to the database and called
the Fill() method of our OleDbDataAdapter, we simply set the DataSource
property of the DataGrid to the DataTable named
"Results". Note that here we use ordinal dsResults.Tables[0]
as opposed to string-referenced binding. Either
works, as we could have also said dsResults.Tables["Results"],
however in applications where performance is of great concern go with the
ordinal method since using the string-referenced collection method comes with a
performance and maintenance price tag.
The formatting of the DataGrid is pretty
straightforward and is accomplished by setting the relevant properties of your
DataGrid to suit the look-and-feel of your application. Some of the more
relevant properties for formatting include: CaptionBackColor,
BackColor, BackgroundColor,
CaptionFont and CaptionText.
In The End...
That is pretty much the main points about the
application. The Visual Studio.NET solution file with all files used in project
is available for you do download. I need to implement a ranking feature for this
application, so that based on game records, the user can also view Player
Rankings based on how much games won and lost. I haven't found a way to do this
yet, and I would welcome any suggestions or solutions to get this done. I tried
to create a player class but that idea did not work out too well. Any more
suggestions? I hope you enjoyed the article and that you learnt something from
it.
Download Source
Adrian Sobers
ajsobers@caribsurf.com