2 Nov 2011

Calling Web Service from Client Script

Design:
<asp:ScriptManager id="SM1" runat="server" EnablePartialRendering="true">
        <Services>
                <asp:ServiceReference  Path="~/TestService.asmx" />
        </Services>
</asp:ScriptManager>

Java Script:
TestService.getDistance(fromto,OnSuccess,OnError);
function OnSuccess(result)
    {
        if (result==null)
        {
            txtKM.value='';
        }
        else
        {
            txtKM.value=result;
         }
    }
    function OnError(error)
    {
             alert("Stack Trace: " + error.get_stackTrace() + "/r/n" +           "Error: " + error.get_message() + "/r/n" +           "Status Code: " + error.get_statusCode() + "/r/n" +           "Exception Type: " + error.get_exceptionType() + "/r/n" +           "Timed Out: " + error.get_timedOut());
    }

Web Service:
Imports System.Web
Imports System.Web.Services
Imports System.Xml
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports SQLDataAccessLayer.DAL
Imports System.Data

<System.Web.Script.Services.ScriptService()> _'Important
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class TestService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function getDistance(ByVal FromTo As String) As String
        Try

            Dim CityFrom, CityTo As Integer
            CityFrom = Val(FromTo.Split("|")(0))
            CityTo = Val(FromTo.Split("|")(1))

            Dim db As DBAccess = New DBAccess

            db.AddParameter("@Case", 7)
            db.AddParameter("@PlaceFrom", CityFrom)
            db.AddParameter("@PlaceTo", CityTo)

            Dim dt As DataTable
            dt = db.ExecuteDataTable(, "usp_LTC")
            If dt.Rows.Count = 0 Then
                Return "123"
            Else
                Return dt.Rows(0)(0)
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Function
End Class

No comments:

Post a Comment