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.Replace Id with Name from Database using jQuery AJAX with C# and VB.Net in ASP.Net <p>Hi&nbsp;<a class="username" rel="Alauddin">Alauddin,</a></p> <p>Too replace the id with name you need to make ajax call and replace the id with database record.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;table id=&#34;12345_AccountInfo&#34;&gt; &lt;tr&gt; &lt;td id=&#34;countries_allowed&#34;&gt; 050,344,360,458,702,764 &lt;/td&gt; &lt;td id=&#34;currencies_allowed&#34;&gt; 1,3,8,13 &lt;/td&gt; &lt;td id=&#34;current_outstanding&#34;&gt; 288,396.01 &lt;/td&gt; &lt;td id=&#34;available_limit&#34;&gt; 11,603.99 &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { var values = $(&#39;#12345_AccountInfo tr&#39;).find(&#39;td&#39;).eq(1).html().trim(); $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/GetDetails&#34;, contentType: &#34;application/json;charset=utf-8&#34;, data: &#39;{ids: &#34;&#39; + values + &#39;&#34; }&#39;, datatype: &#34;json&#34;, success: function (response) { $(&#39;#12345_AccountInfo&#39;).find(&#39;tr td&#39;).eq(1).html(response.d); } }); }); &lt;/script&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Web.Services; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Web.Services </pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">[WebMethod] public static string GetDetails(string ids) { string texts = &#34;&#34;; for (int i = 0; i &lt; ids.Split(&#39;,&#39;).Length; i++) { texts += GetProductName(ids.Split(&#39;,&#39;)[i]) + &#34;,&#34;; } return texts.Remove(texts.Length - 1); } private static string GetProductName(string id) { string name = &#34;&#34;; string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;Select ProductName from Products WHERE ProductId = @Id&#34;; SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { cmd.Connection = con; cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); con.Open(); name = Convert.ToString(cmd.ExecuteScalar()); con.Close(); } return name; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">&lt;WebMethod()&gt; Public Shared Function GetDetails(ByVal ids As String) As String Dim texts As String = &#34;&#34; For i As Integer = 0 To ids.Split(&#34;,&#34;c).Length - 1 texts += GetProductName(ids.Split(&#34;,&#34;c)(i)) &amp; &#34;,&#34; Next Return texts.Remove(texts.Length - 1) End Function Private Shared Function GetProductName(ByVal id As String) As String Dim name As String = &#34;&#34; Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim query As String = &#34;Select ProductName from Products WHERE ProductId = @Id&#34; Dim cmd As SqlCommand = New SqlCommand(query) Using con As SqlConnection = New SqlConnection(conString) cmd.Connection = con cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) con.Open() name = Convert.ToString(cmd.ExecuteScalar()) con.Close() End Using Return name End Function</pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <table id="12345_AccountInfo" border="1"> <tbody> <tr> <td id="countries_allowed">050,344,360,458,702,764</td> <td id="currencies_allowed">Chai,Aniseed Syrup,Northwoods Cranberry Sauce,Konbu</td> <td id="current_outstanding">288,396.01</td> <td id="available_limit">11,603.99</td> </tr> </tbody> </table>https://www.aspforums.net:443/Threads/101888/Replace-Id-with-Name-from-Database-using-jQuery-AJAX-with-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/101888/Replace-Id-with-Name-from-Database-using-jQuery-AJAX-with-C-and-VBNet-in-ASPNet/Tue, 04 Dec 2018 05:37:03 GMT