Dynamic Web Service JSON return

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Web.Script.Serialization
Imports System.Runtime.Serialization.Json
Imports System.IO

Public Class WS
Inherits System.Web.Services.WebService
Private strConn As String

Public Function GetSummaryDynamic(ByVal startrowindex As String, ByVal maximumrows As String, ByVal searchstring As String, ByVal sortby As String) As String

Dim dtResults As New DataTable
Dim ms As New MemoryStream()

Using sqlConn As IDbConnection = CreateConnection(ConnStr(0))
Using sqlCmd As IDbCommand = sqlConn.CreateCommand
sqlCmd.CommandText = “SPP_SUMMARY_ENQ”
sqlCmd.CommandType = CommandType.StoredProcedure
clsCONN.addInputParam(sqlCmd, “startrowindex”, startrowindex, clsCONN.dtType.dbString)
clsCONN.addInputParam(sqlCmd, “maximumrows”, maximumrows, clsCONN.dtType.dbString)
clsCONN.addInputParam(sqlCmd, “searchstring”, searchstring, clsCONN.dtType.dbString)
clsCONN.addInputParam(sqlCmd, “sortby”, sortby, clsCONN.dtType.dbString)
Using sqlReader As IDataReader = sqlCmd.ExecuteReader
End Using
End Using
End Using

Dim strJson As String = DataTableToJSONWithJavaScriptSerializer(dtResults)
Return strJson
Catch ex As Exception
Throw (New ExceptionMsg(GenerateErrNumber(), ex))
End Try
End Function

Public Function DataTableToJSONWithJavaScriptSerializer(ByVal table As DataTable) As String
Dim jsSerializer As JavaScriptSerializer = New JavaScriptSerializer
Dim parentRow As List(Of Dictionary(Of String, Object)) = New List(Of Dictionary(Of String, Object))
Dim childRow As Dictionary(Of String, Object)
For Each row As DataRow In table.Rows
childRow = New Dictionary(Of String, Object)
For Each col As DataColumn In table.Columns
childRow.Add(col.ColumnName, row(col))
Return jsSerializer.Serialize(parentRow)
End Function
#End Region


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s