0

Let's say you have a VB.Net web method with the following prototype:

Public Function HelloWorld() As DataTable

Let's say you have that returned to you in ordinary JavaScript/Ajax. How would you get individual rows out of it, and how would you get individual fields out of those rows (at least assuming it comes from an SQL query - I'm not sure if it makes a difference)? I'm not casting it into any particular type before it gets passed to the success function. To illustrate what I'm talking about, it would be kind of like doing this in AS3:

for each (var table:Object in pEvent.result.Tables) {
    for each (var row:Object in table.Rows) {
        ac.addItem(row["id"]);
    }
}

except that I'm not necessarily looking to loop through them like that - I just want some way to get the information out of them. Thanks!

EDIT: For example, say you have the following function in a VB.Net web service:

<WebMethod()> _
Public Function Test() As DataTable
    Return DBA.OneTimeQuery("SELECT id FROM visits WHERE id = 13")
    // returns a one-row, one-column DataTable with the id field being set to 13
End Function

How would you be able to get 13 out of it once it gets into the JavaScript that's calling the web method? Everything I try just says "[object]".

Panzercrisis
  • 4,590
  • 6
  • 46
  • 85

1 Answers1

1

You do not want to return a DataTable object in your web method. The best approach would be to develop a REST web method that returns JSON that represents the data you want to return, as JavaScript will be able to work with JSON directly. Here is a QA the directs you on how to do this. This is if you are using WCF, which it appears you are. I find it better to use ASP.NET Web API for RESTful web services.

Community
  • 1
  • 1
Kevin Junghans
  • 17,475
  • 4
  • 45
  • 62
  • I've seen somebody else mention on another site that returning DataTables from a web service is "the devil". What causes it to be bad form? – Panzercrisis Oct 30 '12 at 17:30
  • It is not easily serializable to anything useful for a JavaScript client. For more info on why not to do this check out this QA [http://stackoverflow.com/questions/561203/can-i-serialize-a-data-table-or-data-set-to-transfer-over-a-web-service-in-c] – Kevin Junghans Oct 30 '12 at 17:37