Return DataSet from WebMethod in ASP.Net using jQuery Ajax

SajidHussa
 
on Oct 09, 2021 04:43 AM
Sample_118825.zip
2098 Views

In the dataset, I merge two tables i want to return the dataset with these two table and fetch data table1 and table 2 using ajax and webmethod.

in the simplest way I want to return more than one data table in webmethod and fetch data both table as per need

function getbifurcation(woivno,worlid)
{               
    // get tag id
    $.ajax({
        type: "POST",
        url: "AddContractorBillEntry.aspx/getbyfercation",
        data: '{ woivno:"' + woivno + '", worlid:"'+worlid+'" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            //$('#tblworkorderissued tbody').empty();
            var data = JSON.parse(response.d);
            var rw1 = "";
            alert(JSON.stringify(data));                       
        },
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 09, 2021 05:57 AM
on Feb 13, 2022 09:13 AM

Hi SajidHussa,

Refer below example.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<div id="dvDetails"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetCustomers",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var xmlDoc = $.parseXML(response.d);
                var xml = $(xmlDoc);
                var customers = xml.find("Customers");
                var employees = xml.find("Employees");
                var rs = "";
                if (employees.length > 0) {
                    rs += ' <div class="flavour"><h4 class="clrgray">Flavour</h4><div class="flavourcontainer">';
                    for (var i = 0; i < employees.length; i++) {
                        rs += '<a href="#" class="flavourtag">' + $(employees[i]).find('FirstName').text() + '</a>';
                    }
                    rs += '</div></div>';
                }
                $('#dvDetails').html(rs);
            },
            failure: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
    });
</script>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services

Code

C#

[WebMethod]
public static string GetCustomers()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 CustomerID,ContactName FROM Customers GO SELECT EmployeeID,FirstName FROM Employees"))
        {
            cmd.Connection = con;
            DataSet ds = new DataSet();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(ds);
                ds.Tables[0].TableName = "Customers";
                ds.Tables[1].TableName = "Employees";
            }
            return ds.GetXml();
        }
    }
}

VB.Net

<WebMethod()>
Public Shared Function GetCustomers() As String
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("SELECT TOP 10 CustomerID,ContactName FROM Customers GO SELECT EmployeeID,FirstName FROM Employees")
            cmd.Connection = con
            Dim ds As New DataSet()
            Using sda As New SqlDataAdapter(cmd)
                sda.Fill(ds)
                ds.Tables(0).TableName = "Customers"
                ds.Tables(1).TableName = "Employees"
            End Using
            Return ds.GetXml()
        End Using
    End Using
End Function