Hotel Reservation System C# Entity Framework Part 2

This is part 2 of my Hotel Reservation System using C# Entity Framework tutorial. In this article we will create our customer management and implement search function in our form.

Advertisements

Add new form in your project and call it CustomerForm. Then style it like the image below.

Use this names for controls in add group box.

  • txtAddName
  • txtAddAddress
  • txtAddPhone
  • txtAddAge
  • txtAddGender
  • btnAdd

Then use this for controls details group box.

  • txtId
  • txtName
  • txtAddress
  • txtPhone
  • txtAge
  • txtGender
  • btnUpdate

Then add a datagridView and use the default name dataGridView1. Then add a textbox and call it txtSearch and a button name it btnSearch.

Now let’s start with the code.
Add a class and call it Customer.cs inside our Model folder.
Copy this code to your Customer class.

namespace RoomReservationApp.Models
{
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public int Age { get; set; }
        public string Phone { get; set; }
        public string Gender { get; set; }
    }
}

Add Customer to your RoomReservationContext class. Open your RoomReservationContext.cs and change the code to this.

using System.Data.Entity;

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

Since we add Customer in our RoomReservationContext we need to update our database. Build your project then open you Package Manager Console by going to Tools -> Nugets Package Manager -> Package Manager Console.

Run add migration command in your Package Manager Console. Type add-migration then press enter. For name, you can put anything. I named my migration as Add Customers Table then press enter. Then execute the command update-database.

In your CustomerForm double click the form to generate the form load method. Declare and instantiate RoomReservationContext as a class variable and call it context. Then inside your form load method assign the dataGridView1 datasource to context.Customers.ToList().
Your code should look like this.

using RoomReservationApp.Models;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace RoomReservationApp
{
    public partial class CustomerForm : Form
    {
        RoomReservationContext context = new RoomReservationContext();
        public CustomerForm()
        {
            InitializeComponent();
        }
        private void CustomerForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = context.Customers.ToList();
        }
    }
}

Then go to your CustomerForm design view then double click the btnSave. Here we will add a new customer in our database using the data that we get from our textboxes. Then refresh the data in our dataGridView1.

private void BtnAdd_Click(object sender, EventArgs e)
        {
            int age;
            if(!string.IsNullOrEmpty(txtAddName.Text) && !string.IsNullOrEmpty(txtAddAddress.Text) && !string.IsNullOrEmpty(txtAddPhone.Text)
                && !string.IsNullOrEmpty(txtAddGender.Text) && !string.IsNullOrEmpty(txtAddAge.Text) && int.TryParse(txtAddAge.Text, out age))
            {
                context.Customers.Add(new Customer
                {
                    Name = txtAddName.Text,
                    Address = txtAddAddress.Text,
                    Phone = txtAddPhone.Text,
                    Gender = txtAddGender.Text,
                    Age = age
                });
                context.SaveChanges();
                dataGridView1.DataSource = context.Customers.ToList();
                txtAddAddress.Clear();
                txtAddAge.Clear();
                txtAddGender.Clear();
                txtAddName.Clear();
                txtAddPhone.Clear();
                txtSearch.Clear();
            }
        }

Now let’s implement our update function. Go back to your design view and double click the btnUpdate. Then change your code to this.

private void BtnUpdate_Click(object sender, EventArgs e)
        {
            int age;
            if (!string.IsNullOrEmpty(txtId.Text) && !string.IsNullOrEmpty(txtName.Text) && !string.IsNullOrEmpty(txtAddress.Text) && !string.IsNullOrEmpty(txtPhone.Text)
                && !string.IsNullOrEmpty(txtGender.Text) && !string.IsNullOrEmpty(txtAge.Text) && int.TryParse(txtAge.Text, out age))
            {
                var customer = context.Customers.Find(int.Parse(txtId.Text));
                customer.Name = txtName.Text;
                customer.Address = txtAddress.Text;
                customer.Phone = txtPhone.Text;
                customer.Gender = txtGender.Text;
                customer.Age = age;
                context.SaveChanges();
                dataGridView1.DataSource = context.Customers.ToList();
                txtSearch.Clear();
            }
        }

In our btnUpdate, we get the cutomer by id. Then udpate the data of our customer just like how we update the property of our object in C# then call SaveChanges to save our changes to the database.

Then in our design view, click dataGridView1 then in the properties go to events and double click CellClick event.

Then changed the code in CellClick event to this.

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var customer = (Customer)row.DataBoundItem;
                txtId.Text = customer.Id.ToString();
                txtName.Text = customer.Name;
                txtAddress.Text = customer.Address;
                txtPhone.Text = customer.Phone;
                txtGender.Text = customer.Gender;
                txtAge.Text = customer.Age.ToString();
            }
        }

It’s pretty straight forward. Check if there is a selected row in the dataGridView1 then if there is. Get the first item in the selected rows and convert it to Customer type. After that assign to our text fields in the details group box.

For our search function. Double click btnSearch and add this code.

private void BtnSearch_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = context.Customers
                .Where(i => i.Name.Contains(txtSearch.Text))
                .ToList();
        }

Now we are done with our CustomerForm. Open your Form1 then under the Manage menuStrip add Customers.

Double click the Customers menu strip and add this code.

private void CustomersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CustomerForm customerForm = new CustomerForm();
            customerForm.ShowDialog();
        }

Now run your application and you should be able to add and update customers.

That’s all for this part. In the next part we will finish this project.

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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