Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create an autocomplete textbox.

Stored Procedure

SQL
alter proc usp_test_autocomplete --a
     @Distancedetails varchar(50)
as
begin

select top 10 first_name from tbl_user_master where first_name like  @Distancedetails +'%'

end


Front end

i copied the jquery js files. and pasted 3 js files on my aspx page.

XML
<script src="../../jQuery/js/jquery-1.9.1.js" type="text/javascript"></script>
<link href="../../jQuery/css/jquery-ui-1.10.3.custom.min.css" rel="stylesheet" type="text/css" />
<script src="../../jQuery/js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>


then


------------------------------------------------------------------------
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%HeaderHTML("Approving Km", 2, 0);%>
<%--<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"rel="stylesheet"type="text/css"/>
--%>
<link href="../../Utility/Style/TMS.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="../../jQuery/js/jquery-1.9.1.js" type="text/javascript"></script>
<link href="../../jQuery/css/jquery-ui-1.10.3.custom.min.css" rel="stylesheet" type="text/css" />
<script src="../../jQuery/js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">

    $(function() {

        $("#<%=txtautocomplete.ClientID%>").autocomplete({

            source: function(request, response) {

                $.ajax({

                    url: "WebService.asmx/GetEntireNameDetails",
                    data: "{ 'Namedetails': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",

                    success: function(data) {
                    response(data.d);


                    },
                    error: function(result) {

                        alert('incorrect result');
                    }


                });


            }



        });
    });



</script>



------------------------------------------------

i created a web service . When i created web service webservice.cs got created in App_code.

the service i wrote is


C#
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    [WebMethod]
    public List<string> GetEntireNameDetails(string Namedetails)
    {
        DataSet ds = null;
        TMSDatabase TMSComponent = null;
      

        List<string> list = new List<string>();

        try
        {
            TMSComponent = TMSDatabase.CreateInstance();

            ds = TMSComponent.ExecuteStoredProc("usp_test_autocomplete", new TMSDbParameter("@Distancedetails", Namedetails));
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                list.Add(ds.Tables[0].Rows[i]["first_name"].ToString());
            }
        }
        catch
        {

        }

        finally
        {
            if (TMSComponent != null)//databaseComponent is not null
            {
                TMSComponent.CloseConnection();
                TMSComponent = null;
            }


        }
        return list;


    }


}


when i viewed the web service in browser and invoked i was getting the correct result in xml

But when i tried to run the code and pass something in the textbox i did not get the correct result. I got the error message : incorrect result

kindly explain me where i am doing wrong.
Posted
Updated 8-Jan-14 17:33pm
v3
Comments
According to the path you are referring, the service should be on the same folder as the aspx page.
JoCodes 8-Jan-14 23:41pm    
Tadit, add your suggestion as solution since already solved it :)
Thanks a lot JoCodes... :)

Done !!!
anurag19289 8-Jan-14 9:48am    
whenever i create a webservice..the webservice.cs is created inside app_code. How to avoid that
anurag19289 8-Jan-14 9:57am    
i did copy and paste the webservice.asmx(code in the same asmx[didnot create separate file]).

excluded the service which are outside the folder.

clean rebuild

but no result :(

According to the path you are referring, the service should be on the same folder as the aspx page.
 
Share this answer
 
Comments
joginder-banger 9-Jan-14 0:01am    
congratulations...sir for MVP...gud job sir
Thanks a ton joginder-banger... :)

You can call me Tadit instead of sir. :P :D
joginder-banger 9-Jan-14 0:10am    
sir i m junior according your knowledge....so i like this word
No, nobody is junior or senior if you take knowledge as a factor.
Everybody is a learner. We learn new things each day. :)
joginder-banger 9-Jan-14 0:16am    
ok sir..
--Another way to call the webmethod on the aspx.cs itself
--include the namespace

using System.Web.Services;


[WebMethod]
        public static List<string> GetEntireNameDetails1(string Namedetails)
           {
               DataSet ds = null;
               TMSDatabase TMSComponent = null;
               List<string> list = new List<string>();

               try
               {
                   TMSComponent = TMSDatabase.CreateInstance();

                   ds = TMSComponent.ExecuteStoredProc("usp_test_autocomplete", new TMSDbParameter("@Distancedetails", Namedetails));
                   for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                   {
                       list.Add(ds.Tables[0].Rows[i]["first_name"].ToString());
                   }
               }
               catch
               {

               }

               finally
               {
                   if (TMSComponent != null)//databaseComponent is not null
                   {
                       TMSComponent.CloseConnection();
                       TMSComponent = null;
                   }


               }
               return list;
           }

--Same result</string></string></string>
 
Share this answer
 
v2
<![CDATA[<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
XML
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods = "true" runat="server" AsyncPostBackTimeout="900">
    </asp:ScriptManager>
     <asp:AutoCompleteExtender ID="autoextender" runat="server"
                    TargetControlID="txtShuttleNumber"
                    MinimumPrefixLength="1"
                    Enabled="true"
                    EnableCaching="false"
                    CompletionSetCount="25"
                    CompletionInterval="1000"
                    DelimiterCharacters="."
                    FirstRowSelected ="false"
                    ServiceMethod="GetEntireNameDetails">
                    </asp:AutoCompleteExtender>

<asp:textbox id="txtShuttleNumber" runat="server" cssclass="textBox_for6tds" width="150px" xmlns:asp="#unknown">

----code behind--- 
--note : use string prefixText in the method name. Dont change it.
--FirstRowSelected ="true". If true then the first row gets selected.
--DelimiterCharacters="." you can separate the list using these characters

 [System.Web.Script.Services.ScriptMethod()]
 [System.Web.Services.WebMethod]
 public static List<string> GetEntireNameDetails(string prefixText)
    {
        DataSet ds = null;
        TMSDatabase TMSComponent = null;
        List<string> list = new List<string>();

        try
        {
            TMSComponent = TMSDatabase.CreateInstance();

            ds = TMSComponent.ExecuteStoredProc("usp_test_autocomplete", new TMSDbParameter("@Distancedetails", prefixText));
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                list.Add(ds.Tables[0].Rows[i]["first_name"].ToString());
            }
        }
        catch
        {

        }

        finally
        {
            if (TMSComponent != null)//databaseComponent is not null
            {
                TMSComponent.CloseConnection();
                TMSComponent = null;
            }


        }
        return list;

    }

--the method should be public</string></string></string>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900