Hotel Reservation System C# Entity Framework Part 1

To create a Hotel Reservation, we first need to determine our requirements.

  • The user should be able add and update rooms.
  • The user should be able add and update customers.
  • The user should be able to see available rooms.
  • The user should be able to book a customer to a multiple rooms.
  • The user should be able to manage bookings. That includes checking and cancelling of customer reservation.

The list above is the features that we need to complete for this project.

In this part we will create the functionality to add and update rooms.


Let’s start. Create a new Windows Forms App project and call it RoomReservationApp.

Then in solution explorer right click to your project then click Manage Nuget Packages.

install nuget packages

Click the browse tab then look for Entity Framework then click install.


After installing entity framework. Add a new folder in your solution explorer and call it Models. We will put database related classes in this folder.

Inside the Models folder create a new class and call it Room.cs. Then add properties Id(int), Name(string), Price(double) and IsActive(bool) in your Room class.

namespace RoomReservationApp.Models
    public class Room
        public int Id { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public bool IsActive { get; set; }

Then create a new class call RoomReservationContext.cs. This class will inherit a class called DbContext and this will be responsible for connecting to our database.

using System.Data.Entity;

namespace RoomReservationApp.Models
    public class RoomReservationContext : DbContext
        public RoomReservationContext() : base("RoomContext")
        public DbSet<Room> Rooms { get; set; }

If you noticed we passed a “RoomContext” string to our base class. It is the name of our connection string. We haven’t created that yet, so let’s do that. Open app.config in your solution explorer and add our connection string.

    <add name="RoomContext" connectionString="Data Source=(LocalDb)\MSSqlLocalDb;Initial Catalog=RoomDb;Integrated Security=True" providerName="System.Data.SqlClient"/>

Your app.config should look like this now.

app config

Add new form and call it RoomFrm then add controls in your form to look like this.

room form

In add group box change the textbox for name as txtAddName and textbox for price as txtAddPrice then for the add button, call it btnAdd.

In the details group box change the textbox for id as txtId then textbox for name as txtName and textbox for price as txtPrice. Then change the name of update button to btnUpdate.

double click RoomFrm to open code editor and to generate a code for form load. Then declare a class variable called context which is a type of RoomReservationContext.

RoomReservationContext context = new RoomReservationContext()

Your code now should look like this.

form code

Then inside the RoomFrm_Load method. Assign the dataGridView1 datasource to context.Rooms.ToList() to get all the data in our rooms table and display it to dataGridView .

private void RoomFrm_Load(object sender, EventArgs e)
            dataGridView1.DataSource = context.Rooms.ToList();

Go back to design view of your RoomFrm then double click btnAdd button and copy paste this code.

private void BtnAdd_Click(object sender, EventArgs e)
            double price;

            if(!string.IsNullOrEmpty(txtAddPrice.Text) && !string.IsNullOrEmpty(txtAddName.Text) && double.TryParse(txtAddPrice.Text, out price))
                context.Rooms.Add(new Room
                    Name = txtAddName.Text,
                    Price = price,
                    IsActive = true
                dataGridView1.DataSource = context.Rooms.ToList();

In the above code we checked  if the textboxes have a value before executing our insert command. We also used tryParse to check if the text in txtPrice can be converted to double.

Go back to RoomFrm design view then click dataGridView1 then go events. Then double click to CellClick event.

datagrid event

In the CellClick method event copy and paste this code.

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            if (dataGridView1.SelectedRows.Count > 0)
                var row = dataGridView1.SelectedRows[0];
                var room = (Room)row.DataBoundItem;
                txtId.Text = room.Id.ToString();
                txtName.Text = room.Name;
                txtPrice.Text = room.Price.ToString();
                checkActive.Checked = room.IsActive;

Here we checked if there is a selected row. Then if there is a selected row we get the first item in the selected rows and convert it to Room type by casting. Then assign the values to our input fields in details group box.

Now let’s implement the update command. Double click btnUpdate then copy and paste this code.

private void BtnUpdate_Click(object sender, EventArgs e)
            double price;
            if (!string.IsNullOrEmpty(txtId.Text) && !string.IsNullOrEmpty(txtName.Text)
                && !string.IsNullOrEmpty(txtPrice.Text) && double.TryParse(txtPrice.Text, out price))
                var room = context.Rooms.Find(int.Parse(txtId.Text));
                room.Name = txtName.Text;
                room.Price = price;
                room.IsActive = checkActive.Checked;
                dataGridView1.DataSource = context.Rooms.ToList();

Here in our update button. We check if all the input fields have a value and txtPrice value can be converted to double. If all the checks are true then we will get the Room which have an id equals to the value of txtId.Text.

Then after we got the room. We update the value of its properties then call the context.SaveChanges() to save our changes in the database.

Now go to your Form1 then add a MenuStrip. In the Menustrip add Manage then Rooms just like the image below.


Double click Rooms menu then copy paste this code.

private void RoomsToolStripMenuItem_Click(object sender, EventArgs e)
            RoomFrm roomFrm = new RoomFrm();

What we did here, was create a new instance of our RoomFrm then show it using the funtion ShowDialog.

Run your application now and you should now be able to create and update Rooms.

output p1

This is the end of the part1. In the next part we will create the function to manage customer and book a room.

Source code: Source code will be able available after all the parts has been posted.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s