Habit Logger

Introduction

This is 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 any most applications. We think it’s very important to do it from the start of your journey, since everything that will happen from here is just adding complexity to CRUD operations. No matter how complex and fancy the app you’re building is, in the end it all comes down to executing CRUD calls to a database.

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 register one habit.
  • This habit can't be tracked by time (ex. hours of sleep), only by quantity (ex. number of water glasses a day)
  • 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 app should show the user a menu of options.
  • 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.
  • The application should only be terminated when the user inserts 0.
  • You can only interact with the database using raw SQL. You can’t use mappers such as Entity Framework.
  • Your project needs to contain a Read Me file where you'll explain how your app works. Here's a nice example:

Github project with an example of a tidy Read Me file.
Don't panic! I'll help! 😁

What you'll learn

If you have learned the basics of C# following the C# Foundations article, you should know all the basic techniques needed to complete this project. Here’s a list of the things you’ll need to fulfil the requirements:

  1. Taking user input from the console.
  2. Printing messages on the console.
  3. Installing nuget packages (ASP.NET libraries that will help you write your program).
  4. Basic control flow with 'if-else' and 'switch statements'
  5. Connecting to a Sqlite database.
  6. Basics of SQL(the language you use to communicate with the database). If you want to train some SQL before getting started, here's an excellent place.

Tips

  • Test your SQL commands on DB Browser before using them in your program.
  • You can keep all of the code in one single class if you wish.
  • Use a switch statement for the user input menus.
  • 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

  • Let the users create their own habits to track. That will require that you let them choose the unit of measurement of 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.
  • Create a report functionality where the users can view specific information (i.e. how many times the user ran in a year? how many kms?) SQL allows you to ask very interesting things from your database.

Creating The Project

This tutorial requires that you already have set up your .NET environment. If you haven’t yet, check this article out with the first steps. If you absolutely want to use another IDE, let me know.

1. In Visual Studio, choose Create new project. If you’re using Visual Studio Code, let me know if you have difficulties creating the project.
2. Search for Console Application and click Next.
3. Choose a Project Name and a Location and click Next.
3. Choose the latest framework (Net 6 at the time of this tutorial) and click Create.
4. Run your application by clicking on the green play button on the top of the screen.

A CLI (command line interface) will open and print ‘Hello World’ and the application will close itself. That means your application ASP.NET environment and your application are working properly.

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.

Start Coding!

Every time you start your app, it should check if there’s a database. If there isn’t, it will create one along with a table where you’ll store your data. If you delete your database externally, it will always create one. If the database exists, it will move to the next step: take the user input about what you want to do. Something like this:

In ASP.NET C# development, the use of SQL Server is very common. But we won’t be using it here. SQLite is a super lightweight database system and it’s important to get familiar with it before jumping into SQL server.

You’ll need to tell your program to create a sqlite file. You’ll be able to visualise that file externally with the help of a small application. Here's a link to a basic sqlite tutorial.

That’s where your actual code begins! You’re on your own for a while. If you get stuck, keep trying, remember the steps to debug your app/get unstuck. And if you ultimately can’t do it, reach out to our Discord Community or myself and we will help!

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.

Source Control

If this is your first tutorial, you might be tempted to skip this step and start coding. Don’t do it or you’ll learn the hard way. One day after working for many hours on a difficult problem you won’t save your work on a source control repository and for whatever reason all of your work will be lost.

Get into the habit of saving every line of code you ever write. I promise it will save you from many headaches. All serious developers save their work on a backup repository. The most commonly used is Github. Create an account if you haven’t yet.

1. In Visual Studio, go to Git > Create Git Repository
2. Enter your Login Details
3. Click on Create and Push.

✅ Done! Your repository is good to go. Don’t forget to commit and push your changes after every step!

You can take further step and learn how to integrate Github with Visual Studio watching this tutorial:

Code Review

If you want your code to be reviewed by a member of the C# Academy, instead of creating your own repository, follow this article, where you’ll learn how to create a fork from our base review repository.

Next Step: Desktop App

If you have watched the entire C# Foundation course, you have already created a Math Game Desktop app using the amazing .NET MAUI. It will be great practice to build a desktop Habit Tracker App with the same functionality you’ve created for this console app. There will be some challenges, but you’ve already got all the skills necessary. And remember, if you get stuck, reach out on our Discord community and we will help!

Introduction

This is 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 any most applications. We think it’s very important to do it from the start of your journey, since everything that will happen from here is just adding complexity to CRUD operations. No matter how complex and fancy the app you’re building is, in the end it all comes down to executing CRUD calls to a database.

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 register one habit.
  • This habit can't be tracked by time (ex. hours of sleep), only by quantity (ex. number of water glasses a day)
  • 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 app should show the user a menu of options.
  • 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.
  • The application should only be terminated when the user inserts 0.
  • You can only interact with the database using raw SQL. You can’t use mappers such as Entity Framework.
  • Your project needs to contain a Read Me file where you'll explain how your app works. Here's a nice example:

Github project with an example of a tidy Read Me file.
Don't panic! I'll help! 😁

What you'll learn

If you have learned the basics of C# following the C# Foundations article, you should know all the basic techniques needed to complete this project. Here’s a list of the things you’ll need to fulfil the requirements:

  1. Taking user input from the console.
  2. Printing messages on the console.
  3. Installing nuget packages (ASP.NET libraries that will help you write your program).
  4. Basic control flow with 'if-else' and 'switch statements'
  5. Connecting to a Sqlite database.
  6. Basics of SQL(the language you use to communicate with the database). If you want to train some SQL before getting started, here's an excellent place.

Tips

  • Test your SQL commands on DB Browser before using them in your program.
  • You can keep all of the code in one single class if you wish.
  • Use a switch statement for the user input menus.
  • 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

  • Let the users create their own habits to track. That will require that you let them choose the unit of measurement of 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.

  • Create a report functionality where the users can view specific information (i.e. how many times the user ran in a year? how many kms?) SQL allows you to ask very interesting things from your database.

Creating The Project

This tutorial requires that you already have set up your .NET environment. If you haven’t yet, check this article out with the first steps. If you absolutely want to use another IDE, let me know.

1. In Visual Studio, choose Create new project. If you’re using Visual Studio Code, let me know if you have difficulties creating the project.
2. Search for Console Application and click Next.
3. Choose a Project Name and a Location and click Next.
3. Choose the latest framework (Net 6 at the time of this tutorial) and click Create.
4. Run your application by clicking on the green play button on the top of the screen.

A CLI (command line interface) will open and print ‘Hello World’ and the application will close itself. That means your application ASP.NET environment and your application are working properly.

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.

Start Coding!

Every time you start your app, it should check if there’s a database. If there isn’t, it will create one along with a table where you’ll store your data. If you delete your database externally, it will always create one. If the database exists, it will move to the next step: take the user input about what you want to do. Something like this:

In ASP.NET C# development, the use of SQL Server is very common. But we won’t be using it here. SQLite is a super lightweight database system and it’s important to get familiar with it before jumping into SQL server.

You’ll need to tell your program to create a sqlite file. You’ll be able to visualise that file externally with the help of a small application. Here's a link to a basic sqlite tutorial.

That’s where your actual code begins! You’re on your own for a while. If you get stuck, keep trying, remember the steps to debug your app/get unstuck. And if you ultimately can’t do it, reach out to our Discord Community or myself and we will help!

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.

Source Control

If this is your first tutorial, you might be tempted to skip this step and start coding. Don’t do it or you’ll learn the hard way. One day after working for many hours on a difficult problem you won’t save your work on a source control repository and for whatever reason all of your work will be lost.

Get into the habit of saving every line of code you ever write. I promise it will save you from many headaches. All serious developers save their work on a backup repository. The most commonly used is Github. Create an account if you haven’t yet.

1. In Visual Studio, go to Git > Create Git Repository
2. Enter your Login Details
3. Click on Create and Push.

✅ Done! Your repository is good to go. Don’t forget to commit and push your changes after every step!

You can take further step and learn how to integrate Github with Visual Studio watching this tutorial:

Code Review

If you want your code to be reviewed by a member of the C# Academy, instead of creating your own repository, follow this article, where you’ll learn how to create a fork from our base review repository.

Next Step: Desktop App

If you have watched the entire C# Foundation course, you have already created a Math Game Desktop app using the amazing .NET MAUI. It will be great practice to build a desktop Habit Tracker App with the same functionality you’ve created for this console app. There will be some challenges, but you’ve already got all the skills necessary. And remember, if you get stuck, reach out on our Discord community and we will help!

An error has occurred. This application may no longer respond until reloaded. Reload 🗙