Lately I’ve been creating my own projects, because I want to have something to do. I want to be better at programming so I set aside some time to program every day. This way I learn new things. For me it’s not so much what I create, it’s more trying to get a challenge and learning something new.

Previously I made an achievement system for Unity, where I used an SQLite database and used a model of the data to create, update and delete entries. This time around  I decided to create a Windows Form version of the same program. Firstly because I want to see how a non-native-LINQ supported database fares and what the differences might be.

Approaching the problem

Creating something that you’ve done before is always easier the second time around, compared to the first time. This time around I already had graphics that I made for Unity and I had the layout and everything else. So that was the first step, get the layout right. Now I wasn’t very  thorough with the layout, because it wasn’t very important. The important part was getting the data I wanted.

Creating the look in Visual Studio was not that challenging. Getting the buttons for each achievement to be created wasn’t a big issue either. For those who might want to know how that is done, what I did was to have a picture box. Then I put a panel on top which had the dimensions and length I wanted and each of the buttons that spawned had that panel as a control parent and the buttons got their own scroll bar. The result you can see below.

achievementWinForm.png

What Windows Forms does is give you access to the file menu and that helps a lot. Firstly, as opposed to having everything as a button, like for instance reloading and opening up a different database, the file menu is there to do that. So that  is an added “feature”.

The second thing I did was to make sure I could connect to the database. Since the LINQ support for the SQLite libraries didn’t exist, I had to look for how to solve that problem. The solution I found was to use SQL queries instead of LINQ. So I ended up with having to use the SQLiteConnection class, which has a constructor that takes a string that has a Data Source. For me that looked like this:
connection = new SQLiteConnection(“Data Source=.\\..\\..\\Resources\\db.sqlite”);

To be able to use that I needed the SQLiteCommand class, that takes the connection and an sql query string. For instance: “SELECT * FROM ‘AchievementModel'” which runs the query to return all the Achievements. But the SQLCommand does not really run the query, that doesn’t happen until you run SQLReader reader = SQLCommand.ExecuteReader(). Each entry in the table then is made into a dictionary, and in order to get the name, I had to use: string achievementName = (string)reader[“Name”];
Where the [“Name”] is the respective table entry. Since the reader only returns objects, those return values needed to be cast to a different type. After some trial and error, I discovered that the SQLite database only had long as the return values, which was a bit puzzling considering the database I was working with was created with int size variables. Instead of converting all of the data using a conversion that is built-in to C# called unchecked, I decided to just make all the variables that used to be in, into long.

The booleans that I had also was converted into long in the return value, so for those I simply checked if they were zero or one and made a local boolean.

The next step was once I had the data retrieved, I needed to make sure that I could update the data too. Updating as opposed to fetching doesn’t require running a reader, so instead of using:
SQLCommand sqlCommand = new SQLCommand(query);
SQLReader reader = sqlcommand.ExecuteReader();
then loop through the number of rows retrieved. I had to initialize the sqlCommand variable as before, but use sqlCommand.ExecuteNonQuery();
And that would start the update, creation or deletion process. The return value you get is the number of rows updated.

The only thing I really have left on the program is error checking. If a user tries to insert something that already exists into the database. This does exist for Rewards and AchievementGroups, but it is not there for the main achievement. But that is for now fine. The main point was creating a small database driven front end that works without LINQ. That was really what I was going for.

The program was not completed a 100%, but I don’t really had any intention of completely finishing the program. I feel like I have learned what I needed to learn. The rest of the program is about error checking the program. I did make a list of what I need. Basically a check before clicking create, that all the fields are filled in and that the default values aren’t entered (like “Add New Reward” and “Add New Group”). And that if the checkbox unlock achievement is clicked that a value is set. However those things are minor, compared to what I set out to learn.

This to be able to check for null values, empty database etc. Also what is missing is to add a way to insert achievements to the database. I know that’s a major thing to be able to do, but since it is more or less about sending the data you have to the database and just use the SQL query “INSERT INTO ‘Tablename’ (‘column1’, ‘column2’, ‘column3’ … etc) VALUES (item1, item2, item3 etc)”. I already know how to do that, so it’s not learning something new in my opinion.

Going forward

My next project was going to be a simple windows forms with 2 windows. The first window I would have a control mechanism to control the contents of the second window. The problem however is that I wanted to have a way to display hardware accelerated graphics. Windows Forms does not come with a way to control that, it has a thread that runs “internally”, that you can’t get to. And the windows are event based, which means you need to create your own window forms code.

Anyway I’m currently researching the best way to do this. What I am currently thinking is to either start learning node.js and JSON, so that I can use a webbased window and webGL to create what I want. The second option is to have the programs communicate internally through a simple tcplistener. What I want to accomplish is simply to set coordinates in the accelerated window and have that run random effects and transformations and upload images that it can use as a resource and then display that and do simple animations.

I’m leaning towards the networked approach, not because it is the most elegant, it probably isn’t. But more because it would be a nice way to put networking programming in practice.