Ashwini Mann
Ashwini Mann

Reputation: 31

Returning DataSet from WebService and populating a datalist using jQuery $.ajax

Is there any way so that I can populate datalist from the returned DataSet from web service. I want to use $.ajax jquery function. If yes, then please give me a small example.

Upvotes: 1

Views: 5570

Answers (2)

Meryovi
Meryovi

Reputation: 6231

This question is kind of old... but I'll answer anyway.

I would recommend using a custom class, but it is possible to do it using DataSets.

jQuery Code:

  <script type="text/javascript">
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetSomeData",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "xml",
      success: function (msg) {
        $(msg).find('Table').each(function (i, row) {
          alert($(row).find('Field').text());
        });
      }
    });
  </script>

C# Code:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public static string GetSomeData()
    {
        var dataSet = new DataSet();

        // Use proper try-catch!
        string connStr = "Connection String Here";
        using (var conn = new SqlConnection(connStr))
        {
            using (var com = new SqlCommand("select top 5 ID, Field from Table", conn))
            {
                var adp = new SqlDataAdapter(com);
                adp.Fill(dataSet);
            }
        }

        return dataSet.GetXml();
    }

Note: I used the DataSet.GetXml method because the resulting XML is simpler and because you may get some weird errors just returning the DataSet as is.

Upvotes: 3

Sarzniak
Sarzniak

Reputation: 297

No, you can't do it. You must create your custom class with simple types and return it.

Upvotes: 0

Related Questions