Android Tutorial Connect Android to MS SQL Server Tutorial

Status
Not open for further replies.

mkvidyashankar

Active Member
Licensed User
Longtime User
hi erel
thanks for reply, I tried this, but I am getting Internal server error, but it is working fine when i paste the url with query in browser

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

Private quality= "quality" As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private Panel1 As Panel
    Private ListView1 As ListView
    Dim id As Int
    Dim dept As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("Layout1")
    Panel1.SetLayout(0,0,100%x,100%y)
    ListView1.SetLayout(0,30dip,100%x,100%y-30dip)
    fetchdata
'    Dim sqltxt As String
'    If dept="Department: Quality" Then  sqltxt  ="SELECT * FROM [GRAMAPATHA].[dbo].[TrInspectionSheduleHeader] WHERE  IcMonitor =" & id & " order by [DateOfVisit]  desc"
End Sub


Sub fetchdata
    ProgressDialogShow("Fetching list of works")
    ExecuteRemoteQuery("http://89.238.162.147/gpm/query.aspx", quality)
  
  
End Sub

Sub ExecuteRemoteQuery(Query As String, jobname As String)
    Dim job As HttpJob
    Dim sqltxt As String
    id = Main.EmployeeID
    dept=Main.department
    If dept="Department: Quality" Then  sqltxt  ="SELECT * FROM [GRAMAPATHA].[dbo].[TrInspectionSheduleHeader] WHERE  IcMonitor =" & id & " order by [DateOfVisit]  desc"
  
    job.Initialize(jobname, Me)
    job.PostString("http://89.238.162.147/gpm/query.aspx", sqltxt)
End Sub
Sub Activity_Resume

End Sub

Sub JobDone(Job As HttpJob)
      
    ProgressDialogHide
    If Job.Success Then
    Dim res As String
        res = Job.GetString
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        Dim rows As List
        rows = parser.NextArray
        'work with result
        'rows is a List. Each item is a Map with the columns names as keys and the db values as the values.
        For i = 0 To rows.Size - 1
            Log("Rows #" & i)
            Dim m As Map
            m = rows.Get(i)
            ListView1.AddTwoLines(m.Get("MonitorId"),m.Get("DivisionId"))
            Log("col1=" & m.Get("MonitorId")) 'log the values of col1 and col2
            Log("col2=" & m.Get("DivisionId"))
        Next
  
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub

 

mkvidyashankar

Active Member
Licensed User
Longtime User
I think I am getting timeout error

 

mkvidyashankar

Active Member
Licensed User
Longtime User
Hi erel
I checked again, now i am getting log file like this

but I am getting correct result, when i copy and paste the query in browser, url is like this

http://89.238.162.147/gpm/query.aspx?query=SELECT * FROM [GRAMAPATHA].[dbo].[TrInspectionSheduleHeader] WHERE IcMonitor =2411 order by [DateOfVisit] desc
 

MohammadNew

Active Member
Licensed User
Longtime User
Hello Erel, I get error when I run my app.

just I changed the url and table name

B4X:
B4A version: 6.00 BETA #1
Parsing code.    (0.01s)
Compiling code.    (0.20s)
   
ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code.    (0.00s)
Generating R file.    (0.44s)
Compiling generated Java code.    Error
B4A line: 98
Log(Response.GetString(\
javac 1.7.0_25
src\anywheresoftware\b4a\samples\sqlserver\httputilsservice.java:153: error: cannot access ParseException
anywheresoftware.b4a.keywords.Common.Log(_response.GetString("UTF8"));
                                                            ^
  class file for org.apache.http.ParseException not found
 

MohammadNew

Active Member
Licensed User
Longtime User
Hello Erel Now is ok but small problem

when I use this code work fine >
B4X:
    Private ServerUrl As String = "http://hreeef-001-site1.ctempurl.com/test1.aspx?query=select * from tbl1"


but like this does not work
Private ServerUrl As String = "http://hreeef-001-site1.ctempurl.com/test1.aspx"

this all the code
B4X:
Sub Process_Globals
    Private ServerUrl As String = "http://hreeef-001-site1.ctempurl.com/test1.aspx?query=select * from tbl1"
End Sub

Sub Globals

    Private ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
 
    Dim job As HttpJob
    job.Initialize("Job1", Me)
    job.PostString(ServerUrl , "")
End Sub

Sub Activity_Resume
End Sub


Sub Activity_Pause (UserClosed As Boolean)
 
End Sub

Sub JobDone (Job As HttpJob)
    If Job.Success Then
        Dim parser As JSONParser
        Dim response As String = Job.GetString
        parser.Initialize(response)
        Dim rows As List
        rows = parser.NextArray
     
        'work with result
        'rows is a List. Each item is a Map with the columns names as keys and the db values as the values.
        For i = 0 To rows.Size - 1
            Log("Rows #" & i)
            Dim m As Map
            m = rows.Get(i)
            Log("col1=" & m.Get("id")) 'log the values of col1 and col2
            Log("col2=" & m.Get("fname"))
            Log("col3=" & m.Get("address"))
         
            ListView1.AddTwoLines2(m.Get("id") , m.Get("fname") , m.Get("address"))
         
        Next
    End If
    Job.Release
End Sub

and thanks alot
 

Attachments

  • SqlServer.zip
    406.1 KB · Views: 437
Last edited:

MohammadNew

Active Member
Licensed User
Longtime User
Thanks Erel , It works fine when I wrote like this

job.PostString(ServerUrl & "?query=select * from tbl1","")
 

MohammadNew

Active Member
Licensed User
Longtime User
My teacher Erel , sorry about many questions

select and insert are fine

but delete and update are not working

B4X:
job.PostString(ServerUrl & "?query=delete from tbl1 where id='" & EditText1.Text & "'" , "")

job.PostString(ServerUrl & "?query=update tbl1 set id='" & EditText1.Text & "',fname='" & EditText2.Text & "',address='" & EditText3.Text & "' where id='" & EditText1.Text & "'" , "")

this delete all records work fine

job.PostString(ServerUrl & "?query=delete from tbl1" , "")

id = text
 
Last edited:

MohammadNew

Active Member
Licensed User
Longtime User
OK I need RDC but I never used it.

could you give me simple example my teacher. and Thanks alot
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…