ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Return multiple tables in xml format from WebMethod in ASP.Net<p>Refer below code for your reference and implement it as per your code logic.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/GetCustomers&#34;, data: &#39;{}&#39;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: OnSuccess, failure: function (response) { alert(response.d); }, error: function (response) { alert(response.d); } }); }); function OnSuccess(response) { var xmlDoc = $.parseXML(response.d); var xml = $(xmlDoc); // From First Table Customers. var customers = xml.find(&#34;Table&#34;); var row = $(&#34;[id*=gvCustomers] tr:last-child&#34;).clone(true); $(&#34;[id*=gvCustomers] tr&#34;).not($(&#34;[id*=gvCustomers] tr:first-child&#34;)).remove(); $.each(customers, function () { $(&#34;td&#34;, row).eq(0).html($(this).find(&#34;CustomerID&#34;).text()); $(&#34;td&#34;, row).eq(1).html($(this).find(&#34;ContactName&#34;).text()); $(&#34;td&#34;, row).eq(2).html($(this).find(&#34;City&#34;).text()); $(&#34;[id*=gvCustomers]&#34;).append(row); row = $(&#34;[id*=gvCustomers] tr:last-child&#34;).clone(true); }); // From Second Table Employees. var employees = xml.find(&#34;Table1&#34;); var row = $(&#34;[id*=gvEmployees] tr:last-child&#34;).clone(true); $(&#34;[id*=gvEmployees] tr&#34;).not($(&#34;[id*=gvEmployees] tr:first-child&#34;)).remove(); $.each(employees, function () { $(&#34;td&#34;, row).eq(0).html($(this).find(&#34;EmployeeId&#34;).text()); $(&#34;td&#34;, row).eq(1).html($(this).find(&#34;FirstName&#34;).text()); $(&#34;[id*=gvEmployees]&#34;).append(row); row = $(&#34;[id*=gvEmployees] tr:last-child&#34;).clone(true); }); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;asp:GridView ID=&#34;gvCustomers&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; Caption=&#34;&lt;b&gt;Customers&lt;/b&gt;&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerID&#34; HeaderText=&#34;CustomerID&#34; /&gt; &lt;asp:BoundField DataField=&#34;ContactName&#34; HeaderText=&#34;CustomerID&#34; /&gt; &lt;asp:BoundField DataField=&#34;City&#34; HeaderText=&#34;City&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvEmployees&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; Caption=&#34;&lt;b&gt;Employees&lt;/b&gt;&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;EmployeeId&#34; HeaderText=&#34;EmployeeId&#34; /&gt; &lt;asp:BoundField DataField=&#34;FirstName&#34; HeaderText=&#34;FirstName&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><span style="text-decoration: underline;"><strong>C#</strong></span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.BindDummyRow(); } } private void BindDummyRow() { DataTable dummy = new DataTable(); dummy.Columns.Add(&#34;CustomerID&#34;); dummy.Columns.Add(&#34;ContactName&#34;); dummy.Columns.Add(&#34;City&#34;); dummy.Rows.Add(); gvCustomers.DataSource = dummy; gvCustomers.DataBind(); DataTable dummy1 = new DataTable(); dummy1.Columns.Add(&#34;EmployeeId&#34;); dummy1.Columns.Add(&#34;FirstName&#34;); dummy1.Rows.Add(); gvEmployees.DataSource = dummy1; gvEmployees.DataBind(); } [WebMethod] public static string GetCustomers() { string query = &#34;SELECT top 5 CustomerID, ContactName, City FROM Customers GO SELECT top 5 EmployeeId,FirstName FROM Employees&#34;; SqlCommand cmd = new SqlCommand(query); return GetData(cmd).GetXml(); } private static DataSet GetData(SqlCommand cmd) { string strConnString = ConfigurationManager.ConnectionStrings[&#34;conString&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet ds = new DataSet()) { sda.Fill(ds); return ds; } } } } </pre> <p><span style="text-decoration: underline;"><strong>VB.Net</strong></span></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then Me.BindDummyRow() End If End Sub Private Sub BindDummyRow() Dim dummy As DataTable = New DataTable() dummy.Columns.Add(&#34;CustomerID&#34;) dummy.Columns.Add(&#34;ContactName&#34;) dummy.Columns.Add(&#34;City&#34;) dummy.Rows.Add() gvCustomers.DataSource = dummy gvCustomers.DataBind() Dim dummy1 As DataTable = New DataTable() dummy1.Columns.Add(&#34;EmployeeId&#34;) dummy1.Columns.Add(&#34;FirstName&#34;) dummy1.Rows.Add() gvEmployees.DataSource = dummy1 gvEmployees.DataBind() End Sub &lt;WebMethod()&gt; Public Shared Function GetCustomers() As String Dim query As String = &#34;SELECT top 5 CustomerID, ContactName, City FROM Customers GO SELECT top 5 EmployeeId,FirstName FROM Employees&#34; Dim cmd As SqlCommand = New SqlCommand(query) Return GetData(cmd).GetXml() End Function Private Shared Function GetData(ByVal cmd As SqlCommand) As DataSet Dim strConnString As String = ConfigurationManager.ConnectionStrings(&#34;conString&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(strConnString) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using ds As DataSet = New DataSet() sda.Fill(ds) Return ds End Using End Using End Using End Function</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><strong><img src="https://i.imgur.com/qvxjsIA.jpg" alt="" width="332" height="391" /></strong></p>https://www.aspforums.net:443/Threads/115125/Return-multiple-tables-in-xml-format-from-WebMethod-in-ASPNet/https://www.aspforums.net:443/Threads/115125/Return-multiple-tables-in-xml-format-from-WebMethod-in-ASPNet/Thu, 03 May 2018 04:10:28 GMT