KEMBAR78
Room Book | PDF | Parameter (Computer Programming) | Computer Programming
0% found this document useful (0 votes)
32 views7 pages

Room Book

The document contains code for a hotel booking system. It handles booking rooms, retrieving available rooms from a database, and inserting booking details into multiple database tables when a booking is made.

Uploaded by

lorife031592
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Room Book

The document contains code for a hotel booking system. It handles booking rooms, retrieving available rooms from a database, and inserting booking details into multiple database tables when a booking is made.

Uploaded by

lorife031592
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

using System.

Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace it14
{
public partial class Book : Form
{
public Book()
{
InitializeComponent();
}
SqlConnection conn;
SqlCommand cmd;
SqlDataReader dr;
Connection all = new Connection();
int staffID = 0;
string Online = "Online";
int guestID = 0;

private void button1_Click(object sender, EventArgs e)


{

int roomNo = 0;
int room = 0;

if (dateTimePicker1.Value == dateTimePicker2.Value)
{
MessageBox.Show("Please set the date of stay and checkout!");
}
else if (tbFirstname.Text == "")
{
MessageBox.Show("Not Possible!", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else if (tbLastname.Text == "")
{
MessageBox.Show("Not Possible!", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else if (tbContact.Text == "")
{
MessageBox.Show("Not Possible!", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
int x = dataGridView1.CurrentCell.RowIndex;

room =
Convert.ToInt32(dataGridView1.Rows[x].Cells[0].Value.ToString());

for (int y = 0; y < dataGridView3.Rows.Count; y++)


{
roomNo = Convert.ToInt32(dataGridView3.Rows[y].Cells[1].Value);
DateTime checkin =
Convert.ToDateTime(dataGridView3.Rows[y].Cells[3].Value);
DateTime checkout =
Convert.ToDateTime(dataGridView3.Rows[y].Cells[4].Value);
try
{
if (checkin <= dateTimePicker2.Value && checkout >=
dateTimePicker1.Value && roomNo == room)
{

dataGridView3.Rows[y].DefaultCellStyle.BackColor =
Color.Red;
MessageBox.Show("Room not Available", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Error);
dataGridView3.Rows[y].DefaultCellStyle.BackColor =
Color.White;
return;

}
else if (roomNo == 0 && roomNo != room)
{
string rName = "";
decimal amount = 0;
int numberofRoom = 0;
int rNo = 0;
int k = dataGridView1.CurrentCell.RowIndex;

rNo =
Convert.ToInt32(dataGridView1.Rows[k].Cells[0].Value.ToString());
rName =
Convert.ToString(dataGridView1.Rows[k].Cells[1].Value.ToString());
numberofRoom =
Convert.ToInt32(dataGridView1.Rows[k].Cells[3].Value.ToString());
amount =
Convert.ToDecimal(dataGridView1.Rows[k].Cells[4].Value.ToString());

if (tbFirstname.Text == "" || tbContact.Text == "" ||


tbLastname.Text == "")
{
MessageBox.Show("Please Fill The Following");

}
else if (dateTimePicker1.Value > dateTimePicker2.Value)
{
MessageBox.Show("Not Possible!", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
dgRoomBook.Rows.Add(rNo, rName, numberofRoom,
dateTimePicker1.Value, dateTimePicker2.Value ,amount);

}
catch
{
}
}
}
}

private void Book_Load(object sender, EventArgs e)


{
conn = all.getcon();
conn.Open();

cmd = new SqlCommand("Select *From Room", conn);


dr = cmd.ExecuteReader();

while (dr.Read())
{

dataGridView1.Rows.Add(dr[0].ToString(), dr[1].ToString(),
dr[2].ToString(), dr[3].ToString(),dr[4].ToString());
}
dr.Close();

conn = all.getcon();
conn.Open();

cmd = new SqlCommand("Select *From BookingDetails", conn);


dr = cmd.ExecuteReader();

while (dr.Read())
{

dataGridView3.Rows.Add(dr[0].ToString(), dr[1].ToString(),
dr[2].ToString(), dr[3].ToString(), dr[4].ToString());
}
dr.Close();

conn = all.getcon();
conn.Open();
cmd = new SqlCommand("Select StaffID from staff where Status ='" +
Online.ToString() + "'", conn);
dr = cmd.ExecuteReader();

while (dr.Read())
{
staffID = Convert.ToInt32(dr[0].ToString());
}
dr.Close();

private void btnBook_Click(object sender, EventArgs e)


{
int bookNo = 0;
decimal amount = 0;
conn = all.getcon();
conn.Open();

if (tbFirstname.Text == "")
{
MessageBox.Show("Not Possible!", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else if(tbLastname.Text == "")
{
MessageBox.Show("Not Possible!", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else if (tbContact.Text == "")
{
MessageBox.Show("Not Possible!", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else if(textBox1.Text == "")
{
MessageBox.Show("please input total booked room");
}
else
{

cmd = new SqlCommand("Insert Into


Guest(staffID,fName,lName,contactNo) VALUES (@st,@fn,@ln,@cn)", conn);
cmd.Parameters.Add("@st", SqlDbType.Int).Value = staffID;
cmd.Parameters.Add("@fn", SqlDbType.VarChar).Value =
tbFirstname.Text;
cmd.Parameters.Add("@ln", SqlDbType.VarChar).Value =
tbLastname.Text;
cmd.Parameters.Add("@cn", SqlDbType.VarChar).Value =
tbContact.Text;
cmd.ExecuteNonQuery();

cmd = new SqlCommand("Select MAX(guestID) from Guest", conn);


dr = cmd.ExecuteReader();

while (dr.Read())
{

guestID = Convert.ToInt32(dr[0].ToString());
}
dr.Close();

cmd = new SqlCommand("Insert Into Booking(guestID,numberofBookRoom)


VALUES (@gi,@nbr)", conn);
cmd.Parameters.Add("@gi", SqlDbType.Int).Value =
guestID.ToString();
cmd.Parameters.Add("@nbr", SqlDbType.Int).Value = textBox1.Text;
cmd.ExecuteNonQuery();

cmd = new SqlCommand("Select MAX(BookNo) from Booking", conn);


dr = cmd.ExecuteReader();

while (dr.Read())
{

bookNo = Convert.ToInt32(dr[0].ToString());
}
dr.Close();

cmd = new SqlCommand("Insert Into


Billing(bookNo,guestID,numberofRoomBook) VALUES(@bn,@gi,@nbr)", conn);
cmd.Parameters.Add("@bn", SqlDbType.Int).Value = bookNo.ToString();
cmd.Parameters.Add("@gi", SqlDbType.Int).Value =
guestID.ToString();
cmd.Parameters.Add("@nbr", SqlDbType.Int).Value = textBox1.Text;
cmd.ExecuteNonQuery();

int rNo = 0;
int numRoom = 0;
string cIn = "";
string cOut = "";
string roName = "";

for (int row = 0; row < dgRoomBook.Rows.Count; row++)


{
rNo = Convert.ToInt32(dgRoomBook.Rows[row].Cells[0].Value);
roName = Convert.ToString(dgRoomBook.Rows[row].Cells[1].Value);
numRoom = Convert.ToInt32(dgRoomBook.Rows[row].Cells[2].Value);
cIn = Convert.ToString(dgRoomBook.Rows[row].Cells[3].Value);
cOut = Convert.ToString(dgRoomBook.Rows[row].Cells[4].Value);
amount =
Convert.ToDecimal(dgRoomBook.Rows[row].Cells[5].Value);

conn = all.getcon();
conn.Open();

if (rNo != 0)
{
using (cmd = new SqlCommand("Insert Into
BookingDetails(bookNo,roomNo,numberOfRoom,dateStay,checkOut,Amount) VALUES
(@bn,@rn,@nr,@ds,@co,@a)", conn))
{
cmd.Parameters.Add("@bn", SqlDbType.Int).Value =
bookNo;
cmd.Parameters.Add("@rn", SqlDbType.Int).Value = rNo;
cmd.Parameters.Add("@nr", SqlDbType.Int).Value =
numRoom;
cmd.Parameters.Add("@ds", SqlDbType.DateTime).Value =
cIn;
cmd.Parameters.Add("@co", SqlDbType.DateTime).Value =
cOut;
cmd.Parameters.Add("@a", SqlDbType.Decimal).Value =
amount;
cmd.ExecuteNonQuery();
}
using (cmd = new SqlCommand("Insert Into
logBooked(bookNo,roomNo,numberOfRoom,dateStay,checkOut,Amount) VALUES
(@bn,@rn,@nr,@ds,@co,@a)", conn))
{
cmd.Parameters.Add("@bn", SqlDbType.Int).Value =
bookNo;
cmd.Parameters.Add("@rn", SqlDbType.Int).Value = rNo;
cmd.Parameters.Add("@nr", SqlDbType.Int).Value =
numRoom;
cmd.Parameters.Add("@ds", SqlDbType.DateTime).Value =
cIn;
cmd.Parameters.Add("@co", SqlDbType.DateTime).Value =
cOut;
cmd.Parameters.Add("@a", SqlDbType.Decimal).Value =
amount;
cmd.ExecuteNonQuery();
}

}
conn.Close();
conn = all.getcon();
conn.Open();

cmd = new SqlCommand("Select *From BookingDetails", conn);


dr = cmd.ExecuteReader();

dataGridView3.Rows.Clear();
while (dr.Read())
{

dataGridView3.Rows.Add(dr[0].ToString(), dr[1].ToString(),
dr[2].ToString(), dr[3].ToString(), dr[4].ToString());
}
dr.Close();
MessageBox.Show("Book Success!");
tbFirstname.Clear();
tbLastname.Clear();
tbContact.Clear();
tbFirstname.Focus();
dgRoomBook.Rows.Clear();
}
}

private void dataGridView1_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)


{

private void button2_Click(object sender, EventArgs e)


{

private void button2_Click_1(object sender, EventArgs e)


{
MainMenu mm = new MainMenu();
mm.Show();
this.Close();
}
}
}

You might also like