app desktop reservasi
Wed Jun 26 2024 03:58:10 GMT+0000 (Coordinated Universal Time)
Saved by @yeyeyelsa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using reservasi_hotel.Database;
namespace reservasi_hotel
{
public partial class form_reservasi : Form
{
private int id_reservasiroom = 0;
public form_reservasi()
{
InitializeComponent();
}
private void tbreserve_Click(object sender, EventArgs e)
{
Console.WriteLine("Reservation process started.");
if (!ValidateInput())
return;
string nama_pengguna = tbusn.Text;
string atas_nama = tbatasnama.Text;
string alamat = tbaddress.Text;
string telp = tbtelp.Text;
string dipesan_oleh = tbmadeby.Text;
string nama_tipe = cbtipe.SelectedItem.ToString();
string no_kamar = cbnum.SelectedItem.ToString();
DateTime tgl_masuk = dtin.Value;
DateTime tgl_keluar = dtout.Value;
string catatan = tbnote.Text;
try
{
using (SqlConnection conn = DatabaseHelper.GetConnection())
{
conn.Open();
string query = @"
INSERT INTO reservasi_room
(nama_pengguna, atas_nama, alamat, telp, dipesan_oleh, nama_tipe, no_kamar, tgl_masuk, tgl_keluar, catatan)
VALUES
(@nama_pengguna, @atas_nama, @alamat, @telp, @dipesan_oleh, @nama_tipe, @no_kamar, @tgl_masuk, @tgl_keluar, @catatan);
SELECT SCOPE_IDENTITY() AS LastID;";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@nama_pengguna", nama_pengguna);
cmd.Parameters.AddWithValue("@atas_nama", atas_nama);
cmd.Parameters.AddWithValue("@alamat", alamat);
cmd.Parameters.AddWithValue("@telp", telp);
cmd.Parameters.AddWithValue("@dipesan_oleh", dipesan_oleh);
cmd.Parameters.AddWithValue("@nama_tipe", nama_tipe);
cmd.Parameters.AddWithValue("@no_kamar", no_kamar);
cmd.Parameters.AddWithValue("@tgl_masuk", tgl_masuk);
cmd.Parameters.AddWithValue("@tgl_keluar", tgl_keluar);
cmd.Parameters.AddWithValue("@catatan", catatan);
Console.WriteLine($"Executing query: {query}");
// Execute the query to insert reservation and retrieve last inserted ID
// Execute the query to insert reservation and retrieve last inserted ID
object lastIdObj = cmd.ExecuteScalar();
if (lastIdObj != null && lastIdObj != DBNull.Value)
{
id_reservasiroom = Convert.ToInt32(lastIdObj);
MessageBox.Show("Reservation successful!");
ClearForm();
// Hide current form and show payment form with reservation ID
this.Hide();
form_pembayaran f6 = new form_pembayaran(id_reservasiroom);
f6.Show();
}
else
{
MessageBox.Show("Failed to retrieve reservation ID. Please contact support.");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
Console.WriteLine($"Error: {ex.Message}");
}
}
private bool ValidateInput()
{
// Validate all necessary fields for reservation
if (string.IsNullOrWhiteSpace(tbusn.Text) ||
string.IsNullOrWhiteSpace(tbatasnama.Text) ||
string.IsNullOrWhiteSpace(tbaddress.Text) ||
string.IsNullOrWhiteSpace(tbtelp.Text) ||
cbtipe.SelectedItem == null ||
cbnum.SelectedItem == null)
{
MessageBox.Show("Please fill in all fields.");
return false;
}
if (dtin.Value.Date < DateTime.Now.Date)
{
MessageBox.Show("Check-in date cannot be in the past.");
return false;
}
if (dtout.Value.Date <= dtin.Value.Date)
{
MessageBox.Show("Check-out date must be later than the check-in date.");
return false;
}
return true;
}
private void ClearForm()
{
tbusn.Clear();
tbatasnama.Clear();
tbaddress.Clear();
tbtelp.Clear();
cbtipe.SelectedIndex = 0; // Reset dropdown selection
cbnum.SelectedIndex = 0;
dtin.Value = DateTime.Now; // Set to current date
dtout.Value = DateTime.Now; // Set to current date
tbnote.Clear();
}
private void btclear_Click(object sender, EventArgs e)
{
ClearForm();
}
private void btcancel_Click(object sender, EventArgs e)
{
this.Hide();
form_login f2 = new form_login();
f2.Show();
}
}
}



Comments