// method for retrieving booking details public booking getBooking(int id) { // creates new instance of booking booking booking = new booking(); // opens connection to the database using (connection = new SqlConnection(getCString())) { connection.Open(); // select all entries where the ID is as specified string command = String.Format("SELECT * FROM tblBookings WHERE Id = {0}", id.ToString()); Debug.WriteLine(String.Format("Sending SQL command: {0}", command)); // executes the command SqlCommand logincommand = new SqlCommand(command, connection); // gets the returned data SqlDataReader reader = logincommand.ExecuteReader(); // if data exists if (reader.Read()) { // sets booking data booking.id = reader.GetInt32(0); booking.date = reader.GetDateTime(2); booking.UserID = reader.GetInt32(4); booking.period = reader.GetInt32(3); booking.notes = reader.GetString(5); booking.roomID = reader.GetInt32(1); } } // returns booking return(booking); }
// event method for when the form is loaded private void frmNewBook_Load(object sender, EventArgs e) { // creates new instance of the databasehelper object db = new clDB(); help = new clHelper(); if (tempVars.bookingMode == tempVars.modes.View) { foreach (Control c in this.Controls) { if (c is TextBox || c is Button || c is NumericUpDown || c is DateTimePicker) { c.Enabled = false; } } } // defaults edit mode to false modeEdit = false; // if a booking id has been set if (tempVars.editBookingId != -1) { // set all vars to edit mode this.Text = "Edit Booking"; modeEdit = true; editID = tempVars.editBookingId; // gets booking details book = db.getBooking(tempVars.editBookingId); // fills out the form elements with the existing details txtNotes.Text = book.notes; txtPeriod.Value = book.period; txtRoom.Value = book.roomID; dtDate.MinDate = DateTime.MinValue; dtDate.MaxDate = DateTime.MaxValue; dtDate.Value = book.date; // resets the booking id tempVars.editBookingId = -1; } else { // sets all vars to new book mode this.Text = "New Booking"; modeEdit = false; btnDeleteBook.Enabled = false; btnDeleteBook.Visible = false; } // if user has permission to edit booking (either booked themselves or they're an admin if (book != null && (book.UserID == session.userID || session.role == user.roles.Admin)) { // enable ability to delete booking btnDeleteBook.Enabled = true; } else { btnDeleteBook.Enabled = false; } }