Friday, 6 Mar 2026

Display Existing Database Bookings in VB.NET Forms

Connecting to Database and Retrieving Bookings

To display existing bookings when a form loads, start by establishing a database connection. For Access 2010 databases, use the correct connection string:

Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\YourDatabase.accdb"
Using connBookings As New OleDbConnection(connectionString)
    connBookings.Open()
    ' Database operations here
End Using

Always use the Using statement to ensure proper resource disposal. The Microsoft.ACE.OLEDB.12.0 provider specifically targets Access 2010+ databases. If you're using SQL Server, you'd replace this with SqlConnection and a server-based connection string.

Executing SQL Commands

Retrieve booking data with explicit column selection for clarity and maintenance:

Dim sql As String = "SELECT CustomerID, SeatID, BookingDate FROM Bookings"
Using cmd As New OleDbCommand(sql, connBookings)
    Dim adapter As New OleDbDataAdapter(cmd)
    Dim dataSet As New DataSet()
    adapter.Fill(dataSet, "Bookings")
End Using

Avoid SELECT * to prevent unexpected errors if table structures change. Explicit column listing also improves readability for other developers.

Processing Retrieved Data

Validating Data Before UI Updates

Before updating UI elements, verify successful data retrieval:

Dim table As DataTable = dataSet.Tables("Bookings")
If table.Rows.Count > 0 Then
    ' Proceed with UI updates
Else
    MessageBox.Show("No bookings found")
End If

Check Rows.Count immediately after filling the dataset. This simple validation prevents null reference exceptions when accessing data.

Iterating Through Records

Loop through retrieved records to update seat indicators:

For Each row As DataRow In table.Rows
    Dim seatId As Integer = Convert.ToInt32(row("SeatID"))
    Dim pictureBoxName As String = "pictureBox" & seatId
    Dim targetBox As PictureBox = Me.Controls.Find(pictureBoxName, True).FirstOrDefault()
    
    If targetBox IsNot Nothing Then
        targetBox.Image = My.Resources.BookedIcon
    End If
Next

Critical considerations:

  1. Use Controls.Find() with True to search nested containers
  2. Always validate controls exist before property modification
  3. Convert database values to appropriate .NET types

Best Practices for Production Environments

Connection Management

Database connections are limited resources. Follow these rules:

  1. Open connections as late as possible
  2. Close immediately after use
  3. Implement try-catch blocks for error logging
Try
    connBookings.Open()
    ' Execute operations
Catch ex As OleDbException
    LogError("Database error: " & ex.Message)
Finally
    If connBookings.State = ConnectionState.Open Then
        connBookings.Close()
    End If
End Try

Asynchronous Loading

Prevent UI freezing during database operations:

Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Await Task.Run(Sub() LoadBookings())
End Sub

Implementation Checklist

  1. Verify database connection string matches your Access version
  2. Test SQL queries directly in Access first
  3. Handle null values with IsDBNull() checks
  4. Use parameterized queries if adding filters later
  5. Set PictureBox.SizeMode to StretchImage for consistent icons

Advanced recommendation: For large datasets, implement paging through OFFSET-FETCH clauses. For frequently updated data, consider adding a refresh button with incremental loading.

Troubleshooting Common Issues

When icons don't appear:

  1. Confirm picture box names match "pictureBox" + SeatID exactly
  2. Check resource manager references in project properties
  3. Verify image assignments aren't being overwritten elsewhere
  4. Test database permissions with a simple SELECT in Access

"During validation, which step typically causes the most challenges? Share your implementation hurdles below."

Final note: Always separate data access logic into dedicated classes. This foundational pattern enables easier migration to Entity Framework or other ORMs later while keeping your forms clean and maintainable.