SQL Database Error

Hello Xojo community.

I am having a problem with my web app and I have no idea what could be causing the problem.

Test Page

[code]
Dim db As New PostgreSQLDatabase

db.Host = “hidden”
db.DatabaseName = “hidden”
db.UserName = “hidden”
db.Password = “hidden”

Dim ps As PostgreSQLPreparedStatement = _
db.Prepare(“SELECT * FROM pizza_orders;”)

Dim rs As RecordSet = ps.SQLSelect
If db.Error Then
MsgBox("DB Error: " + db.ErrorMessage)
return
Else
Label1.Text = rs.Field(“PizzaType”).StringValue
End If [/code]

When I get to this page I get a DB error with the following message: connection pointer is NULL.
I know my connection details are correct because when I execute the script below it works without any errors.
Ordering Page

Dim db As New PostgreSQLDatabase

db.Host = "hidden"
db.DatabaseName = "hidden"
db.UserName = "hidden"
db.Password = "hidden"

Dim ps As PostgreSQLPreparedStatement = _
db.Prepare("INSERT INTO pizza_orders (Name,PizzaType,Ranch) Values ($1,$2,$3);")

ps.Bind(0, NameBox.Text)
ps.Bind(1, PopupMenu1.Text)
ps.Bind(2, Checkbox1.Value)
ps.SQLExecute

db.SQLExecute("COMMIT")

If db.Connect Then
db.Close
testpage.show
Else
MsgBox(db.ErrorMessage)
Return
End If

You need to connect to the db before any processing.

[code]Dim db As New PostgreSQLDatabase

db.Host = “hidden”
db.DatabaseName = “hidden”
db.UserName = “hidden”
db.Password = “hidden”

If db.Connect = False Then
// Log an error
Break // inspect the error message during debug
Quit
End If

Dim ps As PostgreSQLPreparedStatement = _
db.Prepare(“SELECT * FROM pizza_orders;”)

Dim rs As RecordSet = ps.SQLSelect
If db.Error Then
MsgBox("DB Error: " + db.ErrorMessage)
return
Else
Label1.Text = rs.Field(“PizzaType”).StringValue
End If [/code]

that fixed it. Thank you wayne!

Yes, what Wayne said. But also when you say the second pages works without any errors, that doesn’t mean it really worked. It only means that it didn’t post any errors. And that’s because you aren’t checking for any errors.

You should check for db errors after every operation, not just at the end. In this case, after the db.Prepare then after the PS.SQLSelect. Also, in the first page it looks like you are retrieving multiple rows, so you’ll need loop through them one at a time.