How do I properly access a database in a web application?

I’m pretty new to Xojo and I’m trying my hand at a small web app that I’m going to run here at home. I added an app-wide property called db and defined it as an SQLIteDatabase. It’s a public property. Then, I went into my Session.Open() event and added this code:

Dim db as new SQLiteDatabase

db.DatabaseFile = GetFolderItem(“mydatabase.sqlite”)
if db.Connect() = false then
MsgBox("Connection to the database failed. " + db.ErrorMessage)
end if

Now, I have a form called MainWindow and in that window I have a button called “SigninButton”. In that buttons Action() event, I have the following code:

Dim rs as RecordSet

rs = Session.db.SQLSelect(“SELECT username FROM users WHERE username=’” + trim(UsernameField.Text) + “’”)
if rs.RecordCount > 0 then
end if

I know that the session is connecting to my database properly because it’s not displaying the error message. When I remove the database file from the expected location, it throws an error. But, for some reason, when I try to access the database and query it, I get a NilObjectException.

Anyone know why? How should I properly access a database in the web edition.

Thanks a lot!

If defined db as an app-wide property, using dim db as … will override that name as a local property, so the app-wide property is still nill. Create the database in the App.Open event instead starting with:

db = new SQLiteDatabase // No "dim" required

Thanks, Kem! I ended up modifying my structure a bit so that each user session has its own connection to the database by placing the db property in the Session object then connecting to it from there in the way you suggested. Works fine!