Android Question how to use webservice in asp.net vb from android

Makumbi

Well-Known Member
Licensed User
iam failing to pass parameters to my webservice created in asp.net visual basic 2015
here are my codings please help me

example when i want to insert only one record in the database
when i want to select only one record
when i want to delete


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService

<WebMethod()>
Public Function [Get]() As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT * FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
dt.TableName = "Customers"
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function

<WebMethod()>
Public Sub Insert(name As String, country As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("INSERT INTO Customers (Name, Country) VALUES (@name, @Country)")
cmd.Parameters.AddWithValue("@name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub

<WebMethod()>
Public Sub Update(customerId As Integer, name As String, country As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("UPDATE Customers SET Name = @name, Country = @Country WHERE CustomerId = @CustomerId")
cmd.Parameters.AddWithValue("@CustomerId", customerId)
cmd.Parameters.AddWithValue("@name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub

<WebMethod()>
Public Sub Delete(customerId As Integer)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId")
cmd.Parameters.AddWithValue("@CustomerId", customerId)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
End Class

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Datagriddata.aspx.vb" Inherits="Datagriddata" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerId"
OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
<Columns>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150"/>
</Columns>
</asp:GridView>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="width: 150px">
Name:<br />
<asp:TextBox ID="txtName" runat="server" Width="140" />
</td>
<td style="width: 150px">
Country:<br />
<asp:TextBox ID="txtCountry" runat="server" Width="140" />
</td>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

android code

Sub Getupdates_Click
Dim Job As HttpJob
Job.Initialize("Job1",Me)
Job.Username="..."
Job.Password="..."

Job.Download("http://192.168.1.239/WebServicesula/Service.asmx/Get")
End Sub



Sub JobDone (Job As HttpJob)

Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Select Job.JobName
Case "Job1"

Dim JSON As JSONParser
JSON.Initialize(Job.GetString)

Dim Map1 As Map
Map1 = JSON.NextObject

names.Text = Map1.Get("Country")
'LabelMarking.Text = Map1.Get("marking")
'LabelError.Text = Map1.Get("error")

'print the result to the logs
Log(Job.GetString)
End Select
Else
Log("Error: " & Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release

End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code

codetag001.png

codetag002.png

codetag003.png
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
a http get request should look like this
http://192.168.1.239/WebServicesula/Service.asmx/Update?customerId=123&name=hallo&country=de
use Job.Download2 and see the example there with key,value in array.

but i suggest using POST at service and there a serializable class object, instead of single parameters.
u can make a example function that return a object, it will appear as json string in your android app, u can use it as template.
B4X:
j.PostString(...
j.GetRequest.SetContentType("application/json")

look if u can set get/post attributes in box brackets above Insert/Update
something like this [HttpGet] or [HttpPost]

see also wait for syntax, so u can stay in the scope of one sub.
https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/
 
Last edited:
Upvote 0

Makumbi

Well-Known Member
Licensed User
Good afternoon
i have tried out this but it is giving me error
missing parameter id customerid please help

B4X:
Dim Job As HttpJob
    Dim Job2 As HttpJob

    Job.Username="..."
    Job.Password="..."
    Job2.Username="..."
    Job2.Password="..."
 Dim k As Int
    k="103"
    'Job.Download("https://xy.xy.com/api/barcode/1?barcode=" & BC & "&customer=kro")
    'Job.Download("http://192.168.1.239/WebServicesula/Service.asmx/Update?customerId=123&")
    'Job.Download2("http://192.168.1.239/WebServicesula/Service.asmx/delete?customerId=123&", Array As String(k))
    'Job.GetRequest.SetContentType("application/json")
    
    
    'Send a POST request
    Job2.Initialize("Job2", Me)
    Job2.PostString("http://192.168.1.239/WebServicesula/Service.asmx/Delete?customerId=123&", k)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
its more
B4X:
Job.Download2("http://192.168.1.239/WebServicesula/Service.asmx/delete", Array As String("customerId","123"))
or
B4X:
Job.PostString("http://192.168.1.239/WebServicesula/Service.asmx/Delete",jsonstring)
Job.GetRequest.SetContentType("application/json")
 
Upvote 0
Top