Xojo web page with PostgreSQL

Hello ,
I am new to Xojo.
I ma trying to create a small web page to display data from PostgresQL

I put this code in Session Open Event:

  // Connect to PostgreSQL
  
  Dim db As New PostgreSQLDatabase
  db.Host = "127.0.0.1"
  db.Port = 5432
  db.DatabaseName = "mydb"
  db.Username = "postgres"
  db.Password = "mypwd"
  If db.Connect Then
    //proceed with database operations
  Else
    MsgBox("The connection failed.")
  End If

Now I need to call the session DB from an event page OnOpen.
Here is my code

  Dim sql As String
  sql = "SELECT * FROM  table1 limit 100"
  
  Dim data As RecordSet
  data =Session.db.SQLSelect(sql)

Iam getting an error :

Type Session.Session has no member named db

Please help to underatnd how to use SQL database in a webpage.
If I put the both code in one Page like onOpen page event, it works.
But what I need is to have db object created in session.

Thank you for your help

EDIT : NJP added code tags to aid readability

This has everything to do with SCOPE (not the mouthwash)
In the OPEN event the DB that is created is local that that event and when the event ends that variable disappears

For the DB to be kept across the SESSION you could need to

  1. add a property to the session object - db as PostgreSQLDatabase
    now this will be present in the entire session until the session object is destroyed

  2. change your connection code to

  db = New PostgreSQLDatabase
  db.Host = "127.0.0.1"
  db.Port = 5432
  db.DatabaseName = "mydb"
  db.Username = "postgres"
  db.Password = "mypwd"
  If db.Connect Then
    //proceed with database operations
  Else
    MsgBox("The connection failed.")
  End If

And if what I wrote is mostly greek I’d encourage you to read Introduction to Programming with Xojo

Hello Norman
Thank you so much for your very fast reply.
It’s working now.
I happy to learn Xojo . Great community!

Marcel