Habit Logger

This project has been completed by 342 users

It's time to learn how to integrate C# code with a database! In this very simple task you'll build a very simple app that will teach you how to perform CRUD operations against a real database. These operations are the base of web-development and you’ll be using them throughout your career in most applications. For this reason, we think it’s very important to do it from the start of your journey.

For that you’ll have to learn very simple SQL commands. I know it sounds scary, but you’ll be amazed about how little SQL knowledge you need to build a full-stack app. Don’t worry, we will take you by the hand and by the end you’ll have completed your first fully functioning CRUD app. The most common ways of calling a SQL database with C# are through ADO.NET, Dapper and Entity Framework. We will start by using ADO.NET, because it’s the closest to raw SQL.

If you think this project is too hard for you and you have no idea where to even start, you’re probably right. You might need an extra hand to build a real application on your own. If that’s the case, watch the video tutorial for this project and then come back and try it again on your own. It’s perfectly ok to feel lost, since most beginner courses don’t actually teach you how to build something.

So let’s go!

Requirements

This is an application where you’ll log occurrences of a habit.

This habit can't be tracked by time (ex. hours of sleep), only by quantity (ex. number of water glasses a day)

Users need to be able to input the date of the occurrence of the habit

The application should store and retrieve data from a real database

When the application starts, it should create a sqlite database, if one isn’t present.

It should also create a table in the database, where the habit will be logged.

The users should be able to insert, delete, update and view their logged habit.

You should handle all possible errors so that the application never crashes.

You can only interact with the database using ADO.NET. You can’t use mappers such as Entity Framework or Dapper.

Follow the DRY Principle, and avoid code repetition.

Your project needs to contain a Read Me file where you'll explain how your app works and tell a little bit about your thought progress. What was hard? What was easy? What have you learned? Here's a nice example:

Github project with an example of a tidy Read Me file.

Tips

Read this article about the KISS principle and try to apply it to this project.

Test your SQL commands on DB Browser before using them in your program.

To improve the user's experience, when asking for a date input, give the option to type a simple command to add today's date

You can keep all of the code in one single class if you wish. We'll deal with Object Oriented Programming in the next project

Don't forget the user input's validation: Check for incorrect dates. What happens if a menu option is chosen that's not available? What happens if the users input a string instead of a number?

Challenges

If you already have a bit of experience with programming, we highly recommend you get into the habit of writing unit tests for a few methods in your project. Any method that outputs data and doesn't talk to a database (those are tested in integration tests) can be unit tested. A good example is any method that deals with validation. Here's a quick tutorial.

If you haven't, try using parameterized queries to make your application more secure.

Let the users create their own habits to track. That will require that you let them choose the unit of measurement of each habit. Hot tip: You should not create a table for each habit.

Seed Data into the database automatically when the database gets created for the first time, generating a few habits and inserting a hundred records with randomly generated values. This is specially helpful during development so you don't have to reinsert data every time you create the database.

AI Challenge

Since this is a slightly more advanced challenge you'll be rewarded with 20 extra points. It taps into the future of programming: Artificial intelligence. Can you let the users add records using their voice? For this you'll use Azure's Language Services. You can find a step-by-step tutorial below:

Changing Your Working Directory

This way .NET will build your project in your main folder. By default it builds your project in a bin folder and just to keep things simple we want to avoid that. That will create a Properties folder with a launchsettings.json file containing your configuration information. This is an important step only for applications that use Sqlite because you want the database to be created in the same folder of the application to avoid confusion.

For that, click on the chevron next to the name of your app on the top menu, click on {nameoftheapp} Debug Properties and copy the path of your directory to the 'Working Directory' field. To find out what your path is, you can right click on your project in the Solution Explorer and on “Copy Full Path” or look it up in your Files Explorer. If you’re using Mac/Visual Studio Code, reach out and I’ll tell you how to do it.

Image paragraph
Image paragraph

Video Tutorial

If you’re feeling totally lost, it’s perfectly ok to watch a video tutorial to get you going. Here you’ll learn to connect all the pieces to build a real application. Once you finish it, make sure you try it again on your own without the help of the video so you internalise the newly acquired knowledge.

An unhandled error has occurred. Reload 🗙