B4J Question [ABMaterial] Example - Populate DB Table with Login User ID Data

Harris

Expert
Licensed User
Longtime User
Login Form.
The standard examples show populating tables in each page WHEN the page is inited (in the Main module).
This does not work if you need to login a user (or a company ID, as in my case) and populate your grid with records belonging to each (company or user).

In the Main, I created a process global called "Public comp_id As Long = 0" (by default).
This very simple example gets the "User" input (inp1.text) and queries the table to return a match.
If the returned Map is inited (found a match), it will set the global (Main.comp_id) to the Company ID returned. However... (read next section)

B4X:
Sub msbtn1_Clicked(Target As String)
    Dim mymodal As ABMModalSheet = AppPage.ModalSheet("login")
    Dim inp1 As ABMInput = mymodal.Content.Component("inp1")
    Dim inp2 As ABMInput = mymodal.Content.Component("inp2")
    ' here check the login a page against your login database

    Dim sq As SQL
    sq = DB.pool.GetConnection
  
    Dim m As Map = DBUtils.ExecuteMap(sq, "SELECT id, email FROM members WHERE email = ?", Array As String(inp1.Text))
    sq.Close

    If m.IsInitialized = True Then
       Main.comp_id = m.Get("id")
       Log(" maiin comp: "&Main.comp_id)
    Else
        AppPage.ShowModalSheet("wronginput")
        Return
    End If

    ws.Session.SetAttribute("IsAuthorized", "true")
    ABMShared.NavigateToPage(ws, "./" & InitialPage)
End Sub

Rather than populating the table (grid) when initializing - as the WHERE clause will be 0 (no records), you need to call it on the Page_Ready().

This method (RefeshTable) can be called each time you Open the page, update, insert or delete a record.

B4X:
Sub Page_Ready()
    Log("ready!")
    page.RestoreNavigationBarPosition
    ' Update the table with any new or changed data
    RefeshTable  ' (notice a short delay while results are being retrieved)
End Sub

' show data only WHERE Main.comp_id = logged in users selection (privilege).

Sub RefeshTable
    Dim sq As SQL
    sq = DB.pool.GetConnection
    tblContacts.Clear   ' drop old list or it shall duplicate
    FillcontactosTable(tblContacts , sq.ExecQuery("SELECT PK, Employee_no, First_name, Last_Name, Pin FROM emp WHERE comp_id = "&Main.comp_id&" ORDER BY Last_name "))
    tblContacts.Refresh  ' show new additions, edits or deletes.
    sq.Close
End Sub

Side Note: (where you host many clients in the same DB)


Since this page is only showing records I have right to see (comp_id - set as a global), I don't have to worry about stomping any other data since comp_id is unique.
 

Harris

Expert
Licensed User
Longtime User
Sorry, I should have posted this in the Tutorials section to be consistant... my bad.
 
Upvote 0
Top