Hi mahjoubi,
Please refer below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css"
rel="Stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("[id*=txtName]").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
url: '<%=ResolveUrl("~/Default.aspx/GetNames") %>',
data: "{ 'prefix': '" + request.term + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=hfName]").val(i.item.label);
$.ajax({
type: "POST",
url: '<%=ResolveUrl("~/Default.aspx/GetCountry") %>',
data: "{ 'name': '" + i.item.label + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("[id*=txtCountry]").val(response.d);
},
error: function (response) {
alert(response.responseText);
}
});
},
minLength: 0
});
});
</script>
<table>
<tr>
<td>Enter Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
<td>
<asp:HiddenField ID="hfName" runat="server" />
</td>
</tr>
<tr>
<td>Country:
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" />
</td>
<td></td>
</tr>
</table>
Namespaces
C#
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
VB.Net
Imports System.Web.Services
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Collections.Generic
Code
C#
[WebMethod]
public static string[] GetNames(string prefix)
{
List<string> customers = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT CustomerId, Name FROM Customers WHERE Name LIKE @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(string.Format("{0}-{1}", sdr["Name"], sdr["CustomerId"]));
}
}
conn.Close();
}
}
return customers.ToArray();
}
[WebMethod]
public static string GetCountry(string name)
{
string country = string.Empty;
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT TOP 1 Country FROM Customers WHERE Name = @Name";
cmd.Parameters.AddWithValue("@Name", name);
cmd.Connection = conn;
conn.Open();
country = Convert.ToString(cmd.ExecuteScalar());
conn.Close();
}
}
return country;
}
VB.Net
<WebMethod>
Public Shared Function GetNames(ByVal prefix As String) As String()
Dim customers As List(Of String) = New List(Of String)()
Using conn As SqlConnection = New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT CustomerId, Name FROM Customers WHERE Name LIKE @SearchText + '%'"
cmd.Parameters.AddWithValue("@SearchText", prefix)
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(String.Format("{0}-{1}", sdr("Name"), sdr("CustomerId")))
End While
End Using
conn.Close()
End Using
End Using
Return customers.ToArray()
End Function
<WebMethod>
Public Shared Function GetCountry(ByVal name As String) As String
Dim country As String = String.Empty
Using conn As SqlConnection = New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT TOP 1 Country FROM Customers WHERE Name = @Name"
cmd.Parameters.AddWithValue("@Name", name)
cmd.Connection = conn
conn.Open()
country = Convert.ToString(cmd.ExecuteScalar())
conn.Close()
End Using
End Using
Return country
End Function
Screenshot