Click here to Skip to main content
15,918,617 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Friends,
I'm new to webservice's, I'm trying out autocomplete textbox using jquery. Following is code:
.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    <script src="../JQuery/JQuery.js" type="text/javascript"></script>
    <script type="text/javascript">
        function lookup(inputString) {
            if (inputString.length == 0) {
                $("#suggestions").hide();
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "WebService.asmx/getClientName",
                    dataType: "json",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    success: function(data) {
                        if (data.length > 0) {
                            var datafromserver = data.d.split(",");
                            $("#suggestions").show();
                            $("#suggestionList").html(datafromserver); 'LINE1
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            }
        }
        
    </script>
    <style type="text/css">
    body{
        font-family: Helvetica;
        font-size: 13px;
        color: #000;
        }
    h3{
        margin: 0px;
        padding: 0px;
      }
    .suggestionsBox{
        position: relative;
        left: 0px;
        margin: 0px 0px 0px 0px;
        width: 200px;
        background-color: #7845DD;
        -moz-border-radius: 7px;
        -webkit-border-radius: 7px;
        border: 2px solid #000;
        color: #fff;
        }
    .suggestionList {
        margin: 0px;
        padding: 0px;
        }
    .suggestionList li {
        margin: 0px 0px 3px 0px;
        padding: 3px;
        cursor: pointer;
        }
    .suggestionList li:hover {
        background-color: #DD45CD;
        }

</style>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:TextBox ID="txtAutoComplete" runat="server">
    </div>
    <div class="suggestionsBox" id="suggestions" style="display: none;"></div>
    <div class="suggestionList" id="autoSuggestionsList"></div>
    </form>
    
</body>
</html>


When I run the page, the autocomplete dropdown is not working. Any idea where I'm going wrong?
Is there any issue at LINE1?

Thanks in advance
Posted
Updated 5-May-11 22:40pm
v4

 //this way you cant get record from the dataBase
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
     [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        SqlConnection Con;
        SqlCommand Cmd;
        string[] items;


        [WebMethod]
        public string[] GetStateName(string prefixText)
        {
            try
            {
                Con = new SqlConnection("Your Connection string");
                Con.Open();
                Cmd = new SqlCommand("sp_getValue", Con);
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.AddWithValue("@prefixText", prefixText);
                SqlDataAdapter da = new SqlDataAdapter(Cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                items = new string[dt.Rows.Count];
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    items.SetValue(dr["StateName"].ToString(), i);
                    i++;
                }
            }
            catch (Exception ex)
            {
              throw ex;
            }
            finally
            {
                Con.Close();
            }
            return items;
        }
        }
 
Share this answer
 
Comments
dhage.prashant01 5-May-11 8:51am    
I'm not able to declare
SqlConnection Con;
SqlCommand Cmd;

I have used the namespace, still not able to declate those variables.
dhage.prashant01 6-May-11 4:36am    
ThankU, really helped.
But i have another issue, my autocomplete is not working. I have pasted my code above in question.
 
Share this answer
 
Comments
dhage.prashant01 5-May-11 8:48am    
Nice site, I'm not looking for readymade. I want to make my own code. Any idea?

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