Platforms to show: All Mac Windows Linux Cross-Platform

/Main/Web Edition Examples/Little database sample Web


Required plugins for this example:

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Main/Web Edition Examples/Little database sample Web

This example is the version from Wed, 4th Oct 2022.

Project "Little database sample Web.xojo_binary_project"
Class App Inherits WebApplication
EventHandler Sub Opening(args() as String) #Pragma unused args db = New SQLiteDatabase db.DatabaseFile = SpecialFolder.desktop.Child("registrations.db") If db.DatabaseFile.Exists Then db.Connect // Print "Connected to database." Else db.CreateDatabase // MessageBox "Created database." db.ExecuteSQL "CREATE TABLE Users (Firstname varchar, Lastname varchar, Email varchar)" 'db.CommitTransaction End If Exception d As DatabaseException MessageBox "Database error: "+d.message End EventHandler
Property db As SQLiteDatabase
End Class
Class Session Inherits WebSession
End Class
Class AdminPage Inherits WebPage
Control Liste Inherits WebListBox
ControlInstance Liste Inherits WebListBox
EventHandler Sub SelectionChanged(Rows() as Integer) #pragma unused Rows If Liste.SelectedRowIndex >= 0 Then DeleteButton.Enabled = true EditButton.Enabled = true else DeleteButton.Enabled = false EditButton.Enabled = false end if End EventHandler
End Control
Control AddButton Inherits WebButton
ControlInstance AddButton Inherits WebButton
EventHandler Sub Pressed() dim d as integer = 0 dim c as integer = Runtime.ObjectCount For i As Integer = 0 To c Dim n As String = Runtime.ObjectClass(i) If n.IndexOf("AdminPage") >= 0 Then d = d + 1 end if next EditPage.Add EditPage.show End EventHandler
End Control
Control EditButton Inherits WebButton
ControlInstance EditButton Inherits WebButton
EventHandler Sub Pressed() Dim index As Integer = liste.SelectedRowIndex if index>=0 then Dim RowID As String = liste.RowTagAt(index) EditPage.Edit RowID EditPage.show end if End EventHandler
End Control
Control DeleteButton Inherits WebButton
ControlInstance DeleteButton Inherits WebButton
EventHandler Sub Pressed() dim index as integer = liste.SelectedRowIndex if index>=0 then DeleteButton.Enabled = False EditButton.Enabled = False Dim RowID As String = liste.RowTagAt(index) app.db.ExecuteSQL "DELETE FROM Users WHERE RowID=" + RowID 'app.db.CommitTransaction Liste.RemoveRowAt index end if End EventHandler
End Control
Control RefreshButton Inherits WebButton
ControlInstance RefreshButton Inherits WebButton
EventHandler Sub Pressed() update End EventHandler
End Control
EventHandler Sub Opening() update End EventHandler
Sub Update() EditButton.Enabled = false DeleteButton.Enabled = false liste.RemoveAllRows Dim r As RowSet = app.db.SelectSQL("select RowID,Firstname,Lastname,Email from Users") dim RowID, Firstname, Lastname, Email as string While Not r.AfterLastRow RowID = r.ColumnAt(0).StringValue Firstname = r.ColumnAt(1).StringValue Lastname = r.ColumnAt(2).StringValue Email = r.ColumnAt(3).StringValue liste.AddRow Firstname, Lastname, Email liste.RowTagAt(liste.LastAddedRowIndex)=RowID r.MoveToNextRow wend End Sub
End Class
Class LoginPage Inherits WebPage
Control Label1 Inherits WebLabel
ControlInstance Label1 Inherits WebLabel
End Control
Control Label2 Inherits WebLabel
ControlInstance Label2 Inherits WebLabel
End Control
Control LoginField Inherits WebTextField
ControlInstance LoginField Inherits WebTextField
End Control
Control PasswordField Inherits WebTextField
ControlInstance PasswordField Inherits WebTextField
End Control
Control LoginButton Inherits WebButton
ControlInstance LoginButton Inherits WebButton
EventHandler Sub Pressed() if LoginField.text = "admin" and PasswordField.text = "test" then AdminPage.show else MessageBox "Invalid login." end if End EventHandler
End Control
Control Label3 Inherits WebLabel
ControlInstance Label3 Inherits WebLabel
End Control
End Class
Class EditPage Inherits WebPage
Control Label1 Inherits WebLabel
ControlInstance Label1 Inherits WebLabel
End Control
Control Firstname Inherits WebTextField
ControlInstance Firstname Inherits WebTextField
EventHandler Sub TextChanged() update End EventHandler
End Control
Control Label2 Inherits WebLabel
ControlInstance Label2 Inherits WebLabel
End Control
Control Lastname Inherits WebTextField
ControlInstance Lastname Inherits WebTextField
EventHandler Sub TextChanged() update End EventHandler
End Control
Control Email Inherits WebTextField
ControlInstance Email Inherits WebTextField
EventHandler Sub TextChanged() update End EventHandler
End Control
Control Label3 Inherits WebLabel
ControlInstance Label3 Inherits WebLabel
End Control
Control OKButton Inherits WebButton
ControlInstance OKButton Inherits WebButton
EventHandler Sub Pressed() Dim Firstname As String = Self.Firstname.Text dim Lastname as string = self.Lastname.text dim Email as string = self.Email.text if RowID = "" then // add Dim r As New DatabaseRow r.Column("Firstname") = Firstname r.Column("Lastname") = Lastname r.Column("Email") = Email app.db.AddRow("Users",r) 'app.db.Commit AdminPage.Update AdminPage.show else // edit Dim r As RowSet = app.db.SelectSQL("SELECT RowID, Firstname, Lastname, Email FROM Users WHERE RowID="+RowID) r.EditRow r.ColumnAt(1).StringValue = Firstname r.ColumnAt(2).StringValue = Lastname r.ColumnAt(3).StringValue = Email r.SaveRow 'app.db.Commit AdminPage.Update AdminPage.show end if End EventHandler
End Control
Control CancelButton Inherits WebButton
ControlInstance CancelButton Inherits WebButton
EventHandler Sub Pressed() AdminPage.Update AdminPage.show End EventHandler
End Control
EventHandler Sub Closed() System.DebugLog "Editpage close" End EventHandler
EventHandler Sub Opening() System.DebugLog "Editpage open" End EventHandler
Sub Add() self.RowID = "" Firstname.text = "" Lastname.text = "" Email.text = "" OKButton.Enabled = false End Sub
Sub Edit(RowID as string) self.RowID = RowID dim r as RecordSet = app.db.SQLSelect("SELECT Firstname,Lastname,Email FROM Users WHERE RowID = "+RowID) Firstname.text = r.IdxField(1).StringValue Lastname.text = r.IdxField(2).StringValue Email.text = r.IdxField(3).StringValue End Sub
Sub update() if len(Firstname.text)>0 and len(Lastname.text)>0 and len(email.text)>0 then OKButton.Enabled = true else OKButton.Enabled = false end if End Sub
Property RowID As string
End Class
Sign
End Sign
End Project

See also:

The items on this page are in the following plugins: MBS Main Plugin.


The biggest plugin in space...