Android Question Remote Database Connector (RDC), get results

bakis

New Member
Licensed User
Hello, I am a very beginner in B4a , I managed to connect to a remote MS SQL database using the RDC tutorial.
Now, what i want is just to display the query result to a spinner. If for example the query is "select animal_name fron animals", how can the outcome (the names) be displayed to a spinner1 which I have
created with the designer?
Also,if the query is "select max(animal_id) as idmax from animals " , how can i assign this idmax value to a variable in my code?
Thank you in advance
 

Anser

Well-Known Member
Licensed User
Longtime User
Dear bakis,

I am sure that it is going to be very difficult for anyone to read and understand your code without properly formatting it. You should have used the Insert Code option while you paste some code on the forum.

Why don't you use a Stored Procedure to return the new ID after you have done an insert into the table.

I don't know whether I understood your requirement correctly ?

Do you want to get/know the next possible ID before you insert the new data to the table ?

Regards
Anser
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
codetag001.png

codetag002.png


codetag003.png
 
Upvote 0

bakis

New Member
Licensed User
Dear me, I know the code looks messy the way i have pasted it. Sorry.
I enclose it properly.
Now, in fact, I want to know the NEW id I am going to use BEFORE I write the data.
This is why I First query about the max id in the database and THEN insert the data.
It just doesnt work well for me. It first puts the data with newid=0 and then defines the correct newid..



B4X:
#Region  Project Attributes
    #ApplicationLabel: FINAL04
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape Or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim reqManager As DBRequestManager
    Public ent_last, ent_first, ent_suburb,ent_phone,ent_mobile,ent_mail As String  ' Declares the Value variables
    Public ent_gender As Int,ent_referred As Int,ent_agegroup As Int
    Public ent_birthday,ent_birthmonth As Int,newid As Int,flag1 As Int

End Sub   

Sub Globals
    Private CheckBox1 As CheckBox
    Private CheckBox2 As CheckBox
    Private lblValue01_P1 As Label
    Private st_suburb As EditText
    Private st_last As EditText
    Private st_first As EditText
    Private st_mobile As EditText
    Private st_phone As EditText
    Private st_mail As EditText
    Private referral As Spinner
    Private age As Spinner
    Private st_birthday As Spinner
    Private st_birthmonth As Spinner                                                       
    Private pnlSecondScreen As Panel
    Private Writer As TextWriter
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

   
    Activity.LoadLayout("main2")   
                                    ' Loads "Main" layout file
    If FirstTime Then
       
        flag1=0
       
        If File.ExternalWritable = False Then
        Msgbox("Cannot write on storage card.", "")
        Return
    End If

   
reqManager.Initialize(Me, "http://192.168.1.4:17178")

   
   
       
        ent_last= ""
        ent_first= ""
        ent_suburb= ""
        ent_phone=""
        ent_mobile= ""
        ent_mail = ""               
        ent_gender = 0
        ent_referred = 0
        ent_agegroup=0
       
        Getreferrals  ' selects referrals
        Getagegroup  ' selects agegroup


    End If
End Sub

Sub Getreferrals
    Dim cmd As DBCommand
   
    cmd.Initialize
    cmd.Name = "select_referrals"
    reqManager.Executequery(cmd,0,"cat 1")
   
End Sub
Sub Getagegroup
    Dim cmd As DBCommand
   
    cmd.Initialize
    cmd.Name = "select_agegroups"
    reqManager.Executequery(cmd,0,"cat 2")
   
End Sub

Sub Getnewid 
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "select_maxid"
    reqManager.Executequery(cmd,0,"cat 4")
   
End Sub

Sub checkmobile(mob As String)
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name="select_exists_from_mobile"
    cmd.Parameters=Array As Object(mob)
    reqManager.Executequery(cmd,0,"cat 6")
End Sub

Sub JobDone(Job As HttpJob)
   If Job.Success = False Then
     Log("Error: " & Job.ErrorMessage)
     Msgbox("Communication error","Warning!!")
   Else
     If Job.JobName = "DBRequest" Then
       Dim result As DBResult = reqManager.HandleJob(Job)
      
       Select result.Tag
           Case "cat 2"
              For Each records() As Object In result.Rows
           Dim name As String = records(0) 'or records(result.Columns.Get("name"))
           age.Add(name)
         Next
        
         Case "cat 1"
              For Each records() As Object In result.Rows
           Dim name As String = records(0) 'or records(result.Columns.Get("name"))
           referral.Add(name)
         Next
        
         Case "cat 4" '     finds the max id and adds 1 to make the new id
                   
                For Each records() As Object In result.Rows
           Dim themaxid As String= records(0) 'or records(result.Columns.Get("name"))
         Next
   
         newid=themaxid+1
         Msgbox(newid,"The new client id ")
        
    '----------------------- here starts the mobile check -------------
         Case "cat 6"
                    
              For Each records() As Object In result.Rows
        
         Dim ff As Int=result.Rows.Size
   
        
         If ff > 0 Then   '-------- if mobile exists in db ------
          Dim name3 As String = records(0),name4 As String=records(1) 'or records(result.Columns.Get("name"))
          Msgbox("There is already an entry with this mobile","Another entry found")
          Dim found1 As String, ans2 As Int,ff3 As String
          found1= name3 & " " & name4
        
          Msgbox(found1,"this the entry found")
          ff3="Do you still wish to enter the data as a new client?"
          ans2 = Msgbox2(ff3,"Continue?","Yes","","No",Null)
            '    Msgbox(ans2,"")
                If ans2=DialogResponse.NEGATIVE Then
                    flag1=1
                    Msgbox("the entry is canceled","")
                               
                    Else
                        flag1=0
                        Msgbox("ok, the new client data will be entered in database","")
                       
                            End If

         End If
         Next 

         
         Case "cat 5"
             Msgbox("ok data is entered","")
    End Select
   
       
       End If
     End If
  ' Job.Release
End Sub





Sub GetAnimal(n0 As Int,n1 As String,n2 As String,n3 As String,n4 As String,n5 As String,n6 As String,n7 As String,n8 As Int,n9 As Int,n10 As Int,n11 As Int,n12 As Int)
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "insert_animal"
    cmd.Parameters = Array As Object(n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12)
    reqManager.ExecuteCommand(cmd,"cat 5")
End Sub



Sub Button1_Click
   
Msgbox("start","")

    ent_last=st_last.Text
    ent_first=st_first.Text
    ent_mobile=st_mobile.Text
    ent_mail=st_mail.Text
    ent_phone=st_phone.Text
    ent_suburb = st_suburb.Text
    ent_birthday=st_birthday.SelectedIndex
    ent_birthmonth=st_birthmonth.SelectedIndex
    ent_referred=referral.SelectedIndex
    ent_agegroup=age.SelectedIndex
   
    Getnewid  'here we get the next available client id
   
   
   
Msgbox("now will write to file","store data")
Writer.Initialize(File.OpenOutput(File.DirRootExternal, "Text.txt", True))
Writer.WriteLine(ent_last)
Writer.WriteLine(ent_first)
Writer.WriteLine(ent_suburb)
Writer.WriteLine(ent_mobile)
Writer.WriteLine(ent_phone)
Writer.WriteLine(ent_mail)
Writer.WriteLine(ent_gender)
Writer.WriteLine(ent_referred)
Writer.WriteLine(ent_agegroup)
Writer.WriteLine("end_of_client_data")
Writer.Close


checkmobile(ent_mobile)

Msgbox(flag1,"the flag is:")
Return

If flag1=0 Then
   
GetAnimal(newid,ent_last,ent_first,"",ent_suburb,ent_mobile,ent_phone,ent_mail,ent_referred,ent_gender,ent_agegroup,ent_birthday,ent_birthmonth)
Else
    Return
End If
   
End Sub
 
Upvote 0
Top