
Introduction
In the present scenario, we need our text boxes to act more user friendly and
auto suggest is a feature that makes them more user friendly. Moreover the chance
of making typographical mistakes are also reduced. This article will help you in adding a
simple auto suggest feature using Ajax AutoCompleteExtender.
Background
A service is used to call the data from a database. This autosuggest will populate
top ten records on the basis of the letter entered by you. For example, if we
type "A", the top ten names beginning with the letter "A", then we make it "An",
then top 10 beginning with "An".
Using the Code
Code is populating auto suggest for a company. What we need from the service is
the name of the company and its ID. So here is the method that we are writing in
the service to get the top 10 companies starting with the text typed by the user
in the TextBox.
The name of the service is "CompanyServiceForAutoSuggest" in which exists the method
GetCompanyList which includes two parameters:
- "prefixText" the text to be searched to populate the autosuggest
- "count" the maximum number of records in the auto suggest
The method returns an array string, which
includes company name and its ID.
[WebMethod]
public
String[]
GetCompanyList(String
prefixText,
Int32
count)
{
String[]
strList =
null;
List<companymasterbo> objCompanyList =
new
List<companymasterbo>();
List<string> strCompanyList =
new
List<string>();
short
i;
UtilityServiceClient lObjUtilityServiceClient =
null;
CompanyMasterBO lObjCompanyMasterBO =
null;
lObjCompanyMasterBO =
new
ARGI.Vulcan.ServiceLayer.UtilityService.CompanyMasterBO();
lObjCompanyMasterBO.Company = prefixText.Replace("'","''").ToString().Trim();
lObjCompanyMasterBO.Count = count;
try
{
lObjUtilityServiceClient =
new
UtilityServiceClient();
objCompanyList = lObjUtilityServiceClient.GetCompanyList
(lObjCompanyMasterBO);//
Method to get top 10 company from database
/*
This is my query to get the records
@sql varchar(4000)
set @sql = 'SELECT top ' + convert(varchar(10),@p_Count) +
' comCompanyMasterID,comCompanyName
from dbo.[CompanyMaster] where comCompanyName LIKE
'''+@p_CompanyFirstName+'%'''
exec (@sql) */
if
(objCompanyList.Count >
0)
{
for
(i =
0;
i < objCompanyList.Count; i++)
{
strCompanyList.Add(AjaxControlToolkit.
AutoCompleteExtender.CreateAutoCompleteItem
(objCompanyList[i].Company, Convert.ToString
(objCompanyList[i].CompanyMasterID.ToString())));
}
strList =
new
String[10];
strList = strCompanyList.ToArray();
}
else
{
//strList = new String[1];
//strList[0] = "No Match Found";
}
}
catch
(System.ServiceModel.FaultException FaultExp)
{
throw;
}
catch
(Exception ex)
{
throw;
}
finally
{
if
(lObjUtilityServiceClient !=
null)
lObjUtilityServiceClient =
null;
if
(lObjCompanyMasterBO !=
null)
lObjCompanyMasterBO =
null;
}
return
strList;
}

Kunal AdvaniPosted Jan 2, 2018, 9:44 PM
In the zip folder App_Code/CompanyServiceForAutoSuggest.cs file isnt there
edisonamirtharajPosted Mar 1, 2012, 3:27 AM
ya rlly gd , my sugg:: plz avd more code bcz .net its 80% less cde bt dt be anrg by edi any new thnk -->[email protected]
Abhi KumarPosted Jan 4, 2012, 11:37 PM
Very useful article.
Daisy KrausePosted Jan 4, 2012, 11:30 PM
Very nicely presented.
Emmy DickensPosted Jan 4, 2012, 11:12 PM
Thanks for sharing
Sonakshi SinghPosted Jan 4, 2012, 11:06 PM
keep posting more.
Akash AhlawatPosted Jan 4, 2012, 12:01 PM
My vote of 5 for this article
Jimmy UnderwoodPosted Jan 4, 2012, 11:08 AM
Thanks i need for this topic
Akshay TeotiaPosted Jan 4, 2012, 11:01 AM
Nice work Anuja.
Arjun PanwarPosted Jan 4, 2012, 10:57 AM
Thanks Anuja very nice work