Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. How to convert the datatable into JSON String

How to convert the datatable into JSON String

Scheduled Pinned Locked Moved Web Development
javascriptjsonhelptutorialannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TPS2008
    wrote on last edited by
    #1

    Hi I am using Web Methods and Stateful Classes for DML Operations i.e. (Insert, Update and Delete). Now i want to populate/bind my control from the saved data. I am gettting the datatable from the ajax method and can access it through javascript. I have binded controls synchronously with the values returned through datatable. I want to bind data asynchronously, for that I want to convert datatable into JSON String and return that to javascript. Pls help to for the same.

    S R 2 Replies Last reply
    0
    • T TPS2008

      Hi I am using Web Methods and Stateful Classes for DML Operations i.e. (Insert, Update and Delete). Now i want to populate/bind my control from the saved data. I am gettting the datatable from the ajax method and can access it through javascript. I have binded controls synchronously with the values returned through datatable. I want to bind data asynchronously, for that I want to convert datatable into JSON String and return that to javascript. Pls help to for the same.

      S Offline
      S Offline
      Samer Aburabie
      wrote on last edited by
      #2

      How do you return the data to Javascript ? is it in XML format ? now I think you will have two approaches: First convert the datatable to XML and then to JSON in the code behind and send the JSON presentation back to javascript then bind, the second approach is to convert the datatabe to XML in code behind and then send the XML presentation to Javascript, in Javascript you convert the XML to JSON and then bind. In both approaches the hard part is to convert the XML to JSON either in Javascript or code behind, there are a lot of articles on this subject ... try to search on that and pick the one that suits you.

      Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.

      1 Reply Last reply
      0
      • T TPS2008

        Hi I am using Web Methods and Stateful Classes for DML Operations i.e. (Insert, Update and Delete). Now i want to populate/bind my control from the saved data. I am gettting the datatable from the ajax method and can access it through javascript. I have binded controls synchronously with the values returned through datatable. I want to bind data asynchronously, for that I want to convert datatable into JSON String and return that to javascript. Pls help to for the same.

        R Offline
        R Offline
        Rajdev Ramasamy
        wrote on last edited by
        #3

        You have to loop through the rows to format the data to JSON string. Let's consider an example that your datatable contains two columns: - FirstName - LastName then, the code might be: Note: - Am not giving the exact code

        protected function ConvertDataTableToJSON(byval dt as DataTable)
        Dim strJSON as string
        strJSON = "{"
        for each row as DataRow in dt.Rows
        strJSON = strJSON + "'firstName':" + "'" + row("FirstName") + "',"
        strJSON = strJSON + "'lastName':" + "'" + row("LastName") + "',"
        end for

             strJSON = strJSON + "}"
        
             return strJSON
        

        end function

        Hope this will be useful. Please refer the following link for JSON format: http://en.wikipedia.org/wiki/JSON[^] Thanks, Rajdev KR

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups