Thursday, June 28, 2012

AJAX autocompleteExtender Textbox using web service

In this post i will show how to assign the data to autocomplete textbox from web service in asp.net.Fo this i have used a webservice "FirstAutoComplete.asmx",which has one method GetdeptName() is used to get the data from database based user input.
The ajax tool kit provides a control to perform this kind things./i:e autocomplete extender.It has one property to acces the web servoce which is"ServicePath"
<form id="test" runat="ServeR">
<div>
<asp:ScriptManager ID="sm" runat="Server">
</asp:ScriptManager>
<asp:TextBox od="txttest" runat="Server"></asp:TextBox>
</div>
<Ajaxtest:AutoCompleteExtender ID="ac" runat="Server" TargetControlID="txttest" Servicemethod="GetdeptName" ServicePath="FirstAutoComplete.asmx "
MinimumPrefixLenth="1" EnableCaching="true" CompletionSetCount="12">
</Ajaxtest:AutoCompleteExtender>
</form>
Web service:
Here the web method class has placed app_codefolder.
[System.Web.Script.Serivces.ScriptService()]
public class FirstAutoComplete:System.Web.Services.Webservice
{
[WebMethod]
public string[] GetdeptName(string inputText)
{
        string sqlstr = "select MAINCODE, MAINNAME from MstMainMaster where maincode like 'ddp%' and  MAINNAME like @inputText";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, ConfigurationManager.ConnectionStrings["Con"].ToString());
        da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value  ="%"+ inputText + "%";
        DataTable dt = new DataTable();
        da.Fill(dt);
        string[] outputitems = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            outputitems.SetValue(dr["MAINNAME"].ToString(), i);
            i++;
        }
        return outputitems;
    }

}

No comments:

Bel