Json for mobile

1 posts, 0 answers
  1. L
    L avatar
    298 posts
    Member since:
    Apr 2008

    Posted 18 Aug 2012 Link to this post

    hi

    I have a piece of my web service code here and i wondering if this webservice is good enough to be used with json? How should i call it with json? Thanks a lot

    <WebMethod(Description:="Sales")> _
       Public Function Sales(ByVal Region as String) as SalesClass()

         Dim ds As New DataTable
         ds = getSales(Region)

         Dim newSales as SalesClass() = new SalesClass(ds.Rows.Count - 1) {}

         Dim i As Integer
         For i = 0 To ds.Rows.Count - 1
         newSales(i) = New SalesClass()

         If Not DBNull.Value.Equals(ds.rows(i)(0)) then
         newSales(i).Region = ds.rows(i)(0)
         End If

         If Not DBNull.Value.Equals(ds.rows(i)(1)) then
         newSales(i).Product = ds.rows(i)(1)
         End If

         If Not DBNull.Value.Equals(ds.rows(i)(2)) then
         newSales(i).Amount = ds.rows(i)(2)
         End If

         If Not DBNull.Value.Equals(ds.rows(i)(3)) then
         newSales(i).TimeFrame = ds.rows(i)(3)
         End If

         Next

         Return newSales

       End Function

       Public Class SalesClass

        Public Region as System.String
        Public Product as System.String
        Public Amount as System.Decimal
        Public TimeFrame as System.String

       End Class

       Public function getSales(Region as String) as Datatable

        Dim cn as new SqlConnection("Data Source=local;Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;password=password")
        Dim cmd As new SqlCommand("SELECT Region, Product, Amount ,TimeFrame   FROM dbo.Transaction   where Region = @Region", cn)

        Dim p0 As New SqlParameter
        p0.ParameterName ="@Region"
        p0.Value = Region
        cmd.Parameters.Add(p0)

        cn.Open()
        Dim da As New SqlDataAdapter(cmd)
        Dim dtResults As New DataTable

        Try
        da.Fill(dtResults)
        Catch ex As Exception
        da.Dispose()
        dtResults.Dispose()
        MsgBox("Something went wrong - please check the query string")
        Finally
        cn.Close()
        da.Dispose()
        End Try

        Return dtResults

       End Function

Back to Top