Replace Id with Name from Database using jQuery AJAX with C# and VB.Net in ASP.Net

Last Reply 9 days ago By dharmendr

Posted 10 days ago

Hi,

I have to show value of td id currencies_allowed instead of  051,050,052,840 to Armenian Dram, Bangladeshi Taka, Barbados Dollar, United States Dollar 

Value need to match from db where  051,050,052,840 corresponding values to Armenian Dram, Bangladeshi Taka, Barbados Dollar, United States Dollar

I got value from other application like API. Then I parse as my way. I receive the value as table and send to aspx page for print.

Ltable2.Text = output2;// output2 value as table getting from API

View Page source in my browser what coming as Ltable2.Text = output2

<table id="12345_AccountInfo">
    <tr>
        <td id="countries_allowed">
            050,344,360,458,702,764
        </td>
        <td id="currencies_allowed">
            051,050,052,840
        </td>
        <td id="current_outstanding">
            288,396.01
        </td>
        <td id="available_limit">
            11,603.99
        </td>
    </tr>
</table>

 

You are viewing reply posted by: dharmendr 9 days ago.
Posted 9 days ago

Hi Alauddin,

Too replace the id with name you need to make ajax call and replace the id with database record.

Check this example. Now please take its reference and correct your code.

Database

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

Download Northwind Database

HTML

<table id="12345_AccountInfo">
    <tr>
        <td id="countries_allowed">
            050,344,360,458,702,764
        </td>
        <td id="currencies_allowed">
            1,3,8,13
        </td>
        <td id="current_outstanding">
            288,396.01
        </td>
        <td id="available_limit">
            11,603.99
        </td>
    </tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        var values = $('#12345_AccountInfo tr').find('td').eq(1).html().trim();
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetDetails",
            contentType: "application/json;charset=utf-8",
            data: '{ids: "' + values + '" }',
            datatype: "json",
            success: function (response) {
                $('#12345_AccountInfo').find('tr td').eq(1).html(response.d);
            }
        });
    });
</script>

Namespaces

C#

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

VB.Net

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

Code

C#

[WebMethod]
public static string GetDetails(string ids)
{
    string texts = "";
    for (int i = 0; i < ids.Split(',').Length; i++)
    {
        texts += GetProductName(ids.Split(',')[i]) + ",";
    }

    return texts.Remove(texts.Length - 1);
}

private static string GetProductName(string id)
{
    string name = "";
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "Select ProductName from Products WHERE ProductId = @Id";
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@Id", id);
        con.Open();
        name = Convert.ToString(cmd.ExecuteScalar());
        con.Close();
    }

    return name;
}

VB.Net

<WebMethod()>
Public Shared Function GetDetails(ByVal ids As String) As String
    Dim texts As String = ""
    For i As Integer = 0 To ids.Split(","c).Length - 1
        texts += GetProductName(ids.Split(","c)(i)) & ","
    Next

    Return texts.Remove(texts.Length - 1)
End Function

Private Shared Function GetProductName(ByVal id As String) As String
    Dim name As String = ""
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "Select ProductName from Products WHERE ProductId = @Id"
    Dim cmd As SqlCommand = New SqlCommand(query)
    Using con As SqlConnection = New SqlConnection(conString)
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@Id", id)
        con.Open()
        name = Convert.ToString(cmd.ExecuteScalar())
        con.Close()
    End Using

    Return name
End Function

Output

050,344,360,458,702,764 Chai,Aniseed Syrup,Northwoods Cranberry Sauce,Konbu 288,396.01 11,603.99