Hi MohmdNader,
I have created sample for your reference. Refer the below sample and change as per your requirement.
SQL
CREATE TABLE EmployeesInfo(ID INT,EmpName VARCHAR(10),Branch CHAR(1))
INSERT INTO EmployeesInfo VALUES(1,'Mohmd', 'A')
INSERT INTO EmployeesInfo VALUES(2,'Ali', 'B')
INSERT INTO EmployeesInfo VALUES(3,'Khalid','A')
INSERT INTO EmployeesInfo VALUES(4,'dharmendr','C')
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 rel="Stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css" />
<script type="text/javascript">
$(function () {
$("#TxtEmpNameVar").autocomplete({
source: function (request, response) {
$.ajax({
url: 'NewBook.aspx/GetEmpName',
data: "{ 'prefixEmp': '" + request.term + "', 'EmpSec': '" + $("#lblBranch").html() + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item,
val: item
};
}))
} else {
//If no records found, set the default "No match found" item with value -1.
response([{ label: 'No results found.', val: -1}]);
}
}
});
},
select: function (e, u) {
//If the No match found" item is selected, clear the TextBox.
if (u.item.val == -1) {
//Clear the AutoComplete TextBox.
$(this).val("");
return false;
}
}
});
});
</script>
<asp:Label ID="lblBranch" Text="A" runat="server" />
Enter search term:
<input type="text" id="TxtEmpNameVar" />
C#
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static string[] GetEmpName(string prefixEmp, string EmpSec)
{
List<string> Emps = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select EmpName from EmployeesInfo where EmpName like '%' + @SearchTextEmp + '%' AND Branch = @Branch";
cmd.Parameters.AddWithValue("@SearchTextEmp", prefixEmp);
cmd.Parameters.AddWithValue("@Branch", EmpSec);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Emps.Add(sdr["EmpName"].ToString());
}
}
conn.Close();
}
}
return Emps.ToArray();
}
VB.Net
<System.Web.Services.WebMethod()>
<System.Web.Script.Services.ScriptMethod()>
Public Shared Function GetEmpName(prefixEmp As String, EmpSec As String) As String()
Dim Emps As New List(Of String)()
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("conn").ConnectionString
Using cmd As New SqlCommand()
cmd.CommandText = "select EmpName from EmployeesInfo where EmpName like '%' + @SearchTextEmp + '%' AND Branch = @Branch"
cmd.Parameters.AddWithValue("@SearchTextEmp", prefixEmp)
cmd.Parameters.AddWithValue("@Branch", EmpSec)
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Emps.Add(sdr("EmpName").ToString())
End While
End Using
conn.Close()
End Using
End Using
Return Emps.ToArray()
End Function
Screenshot