• We just launched and are currently in beta. Join us as we build and grow the community.

Hotel Reservation System Reusable Code

noAntiVirus

Tech Trends Analyst
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
142
Likes
178
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Submitted by
admin

on
Sun, 04/05/2009 - 14:27

In this article I will explain to you on how you can use some part of the code in Hotel Reservation System (VB.NET).

Introduction

In this article I will explain the important procedure or function that I used in Hotel Reservation System. I wrote this article in the purpose that you will be able to use some of the useful code here in my application. May it be a payroll system, inventory system, or any application that have the same concept like filling a Listview or Combobox.

Background

I made an upgrade with my Hotel Reservation System from VB 6.0 to VB.NET. Just a note though for VB 6.0 programmer –kindly continue with what you are doing right now if you feel you are more productive in version 6.0. But if you want to upgrade your knowledge, then I recommend using the new version which is VB.Net.

Using the Software

My Hotel Reservation System will help you manage a collection of data in your hotel. Moreover, you can record a reservation, check in, check out, payments, etc.
Since the purpose of this article is to teach you the importance of source code within this program, I attached here a link where you can read simple tutorial from my website at Free Hotel Management System Manual.

Using the Code

May be this code isn’t new to all of you but I do believe there are still programmer out there who needs this. I have three important code snippets to share which I used in this program – Hotel Reservation System.

Filling a ListView Control

Overview

The Windows Forms ListView control displays a list of items with icons. You can use a list view to create a user interface like the right pane of Windows Explorer. The control has four view modes: LargeIcon, SmallIcon, List, and Details. Source: Microsoft Visual Studio 2008 Documentation

In the previous paragraph, listview is used to display a list of items. So, in my application I used it to display a list of records from a table with a customize column.
To fill a listview in my program you call it like:

FillListView(

lvList, GetData(

sSql)

)

Where lvList in the first parameter is a ListView control in the FillListView procedure. The second parameter – GetData(sSql) will first call the GetData function and returns a data using OleDbDataReader.

Before we call the FillListView procedure, we will call first the procedure named FillList. In this procedure you can customize the number of columns that you want to show in a listview.

Here’s the code for FillList procedure:

  1. Public

    Sub

    FillList(

    )
  2. With

    lvList
  3. .

    Clear

    (

    )

  4. .

    View

    =

    View.

    Details
  5. .

    FullRowSelect

    =

    True
  6. .

    GridLines

    =

    True
  7. .

    Columns

    .

    Add

    (

    &

    quot;Room Number&

    quot;, 90

    )
  8. .

    Columns

    .

    Add

    (

    &

    quot;Room Type&

    quot;, 120

    )
  9. .

    Columns

    .

    Add

    (

    &

    quot;Status&

    quot;, 90

    )

  10. FillListView(

    lvList, GetData(

    sSql)

    )
  11. End

    With
  12. End

    Sub

This procedure can be found in any form that has a listview control to list all records from a table. The only difference is that you can set the properties for every column in your listview. Of course, FillList procedure is being called at the form load event. So it will load the data from a table into the listview control before the form is displayed totally.

The following is the code for FillListView procedure:

  1. Public

    Sub

    FillListView(

    ByRef

    lvList As

    ListView, ByRef

    myData As

    OleDbDataReader)
  2. Dim

    itmListItem As

    ListViewItem

  3. Dim

    strValue As

    String

  4. Do

    While

    myData.

    Read
  5. itmListItem =

    New

    ListViewItem(

    )
  6. strValue =

    IIf

    (

    myData.

    IsDBNull

    (

    0

    )

    , &

    quot;&

    quot;, myData.

    GetValue

    (

    0

    )

    )
  7. itmListItem.

    Text

    =

    strValue

  8. For

    shtCntr =

    1

    To

    myData.

    FieldCount

    (

    )

    -

    1
  9. If

    myData.

    IsDBNull

    (

    shtCntr)

    Then
  10. itmListItem.

    SubItems

    .

    Add

    (

    &

    quot;&

    quot;)
  11. Else
  12. itmListItem.

    SubItems

    .

    Add

    (

    myData.

    GetValue

    (

    shtCntr)

    )
  13. End

    If
  14. Next

    shtCntr

  15. lvList.

    Items

    .

    Add

    (

    itmListItem)
  16. Loop
  17. End

    Sub

Because ListView from the calling procedure is being referenced in this procedure, you do not need to call the name of a form anymore, say form1.lvList. Instead you fill the listview with:

lvList.

Items

.

Add

(

itmListItem)

Points of interest

Since I am using a listview to retrieve the data from the table especially in my masterfiles, I don’t need to create the same code once again to fill the listview. All you need is to pass the name of a listview in the first parameter and the SQL string returned by OleDbDataReader to fill the listview control.

Filling a Combobox Control

Overview

The Windows Forms ComboBox control is used to display data in a drop-down combo box. By default, the ComboBox control appears in two parts: the top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one. Source: Microsoft Visual Studio 2008 Documentation

ComboBox is again another control that needs not to be filled with data using a redundant code. In this code snippet, we can fill the combobox with just one procedure by passing parameters from the calling form.

The procedure is called using this syntax:

FillCombobox(

cboCountry, "SELECT * FROM Countries"

, "Countries"

, "Country"

, "CountryID"

)

Notice that there are 5 parameters that will be passed to the FillCombobox procedure. The first parameter which is cboCountry is the name of a combobox in your form that will be filled with data from Countries table in the second parameter, the third is just a dummy, which is actually the name of the table to be used in the datasource in the FillCombobox procedure, the fourth (Country) is the name of a field that has an actual data. The fifth parameter (CountryID) will be used to tag the actual data in Country field.

The FillCombobox Procedure:

  1. Public

    Sub

    FillCombobox(

    ByVal

    cboCombo As

    ComboBox, ByVal

    sSQL As

    String

    , ByVal

    strTable As

    String

    , ByVal

    strDisplayMember As

    String

    , ByVal

    strValueMember As

    String

    )
  2. Dim

    cnHotel As

    OleDbConnection
  3. cnHotel =

    New

    OleDbConnection

  4. Try
  5. With

    cnHotel
  6. If

    .

    State

    =

    ConnectionState.

    Open

    Then

    .

    Close

    (

    )

  7. .

    ConnectionString

    =

    cnString
  8. .

    Open

    (

    )
  9. End

    With

  10. Dim

    da As

    OleDbDataAdapter =

    New

    OleDbDataAdapter(

    sSQL, cnHotel)
  11. Dim

    dt As

    New

    DataSet

  12. da.

    Fill

    (

    dt, strTable)

  13. cboCombo.

    DataSource

    =

    dt.

    Tables

    (

    strTable)

    .

    DefaultView
  14. cboCombo.

    DisplayMember

    =

    strDisplayMember
  15. cboCombo.

    ValueMember

    =

    strValueMember
  16. Catch

    ex As

    Exception
  17. MessageBox.

    Show

    (

    ex.

    Message

    )
  18. Finally
  19. cnHotel.

    Close

    (

    )
  20. End

    Try
  21. End

    Sub

As you can see in this code:

  1. cboCombo.

    DataSource

    =

    dt.

    Tables

    (

    strTable)

    .

    DefaultView
  2. cboCombo.

    DisplayMember

    =

    strDisplayMember
  3. cboCombo.

    ValueMember

    =

    strValueMember

The 3 properties of cboCombo will use the parameter that is being passed from the calling procedure. The best part here is the DisplayMember and ValueMember which actually holds the data. I remember that filling a combobox with data in version 6.0 of visual basic will certainly takes a lot of code. But now it’s a lot easier with vb.net.

DisplayMember is actually the dummy field for this combobox because the actual data that is saved in the database is the value from ValueMember.

Example:

ValueMember (CountyID) DisplayMember (Country)
1 United States
2 India
3 Canada

This means that only the value of the CountryID will be saved in the table and not the Country.

Counting the Number of Records in a Table

In visual basic 6.0, if you are going to determine the number of records in a recordset, all you have to do is add a code like rs.recordcount, where rs is the name of a variable that holds the data.

However, in VB.NET this functionality has been removed. So, how can we figure out the number of recordset then?

The following is an example again from hotel reservation system.

  1. <

    p>

    Dim

    sqlQRY As

    String

    =

    "SELECT * FROM [Room_Rates] WHERE RoomNumber = "

    &

    RoomNumber &

    " AND RateTypeID = "

    &

    cboRateType.

    SelectedValue
  2. Dim

    cmd As

    OleDbCommand =

    New

    OleDbCommand(

    sqlQRY, cnHotel)
  3. Dim

    RecordCount As

    Integer

  4. RecordCount =

    CountRows(

    "SELECT Count(*) FROM [Room_Rates]"

    )

    </

    p>

This is very important if you want to determine first if the recordset contains a data to avoid errors later in your code.
This is just a few of the useful code which can be found in Hotel Reservation System.

The following are some of the common question which I found very useful.

  1. How to subtract a date in DatePicker
    dtpDateOut.Value.AddDays(-1)
  2. How to add days in a DatePicker

    dtpDateOut.Value = dtpDateIn.Value.AddDays(1)

To Do List

  1. Add Login Form
    1. Add Toolbar
    2. Fix Room Inventory
    3. Reports
      • Folio Report
      • Reservation Report
      • Account Receivable Report
      • Check In Guest Report
      • Check Out Report
      • Guest List Report
      • Other Charges Report
      • Room History

 

439,009

316,008

316,017

Top