Creating web service in VB.NET that outputs to JSON raw data

Here is a quick tutorial on creating a web service in VB.NET that output the data in JSON.
Imports System
Imports System.Web
Imports System.Collections
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
Imports System.ComponentModel
 
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class config
    Inherits System.Web.Services.WebService
 
    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function getConfigurationValueByKey(ByVal strKeyName As String) As String
        Dim sqlConnection As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
        sqlConnection.Open()
        Dim sqlCommand As SqlCommand = New SqlCommand("SELECT * FROM table1 WHERE column1 ='" + strKeyName + "';", sqlConnection)
        Dim sqlDataset As DataSet = New DataSet()
        Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
 
        sqlDataAdapter.Fill(sqlDataset)
        sqlConnection.Close()
 
        Dim MArray()() As String = New String(sqlDataset.Tables(0).Rows.Count)() {}
        Dim i As Integer = 0
 
        For Each rs As DataRow In sqlDataset.Tables(0).Rows
            MArray(i) = New String() {rs("KeyName").ToString(), rs("ValueName").ToString()}
            i = i + 1
        Next
 
        Dim js As JavaScriptSerializer = New JavaScriptSerializer()
        Dim sJSON As String = js.Serialize(MArray)
        Return sJSON
    End Function
 
End Class
Output will be something like this.
  <?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://tempuri.org/">[["key1","value1"],["key2","value2"]]</string>
FacebookTwitterGoogle+Share

5 Comments

  1. Does this work with $.each? It keeps splitting the string into single letters.

    TIA

    Reply

  2. I must say, useful information for users especailly for website designers.

    Reply

  3. @Joe
    It is pretty simple. The above service should give JSON output similar to the link below. You can use $.getJSON and point it to the URL of web service given you have a basic http binding.

    http://api.jquery.com/jQuery.getJSON/

    Reply

  4. This is very good article. Thanks alot.
    And this returnd this format:

    [[“key1″,”value1”],[“key2″,”value2”]]

    But Ajax call requird this format(name/value)

    [
    [“key1”: “value1”],
    [“key2″:”value2”]
    ]

    If you can explain, that will be great,
    Thanks a lot.

    Reply

  5. can you please tell me how to call this webservice on nbutton click
    i am new in webservice

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*