Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi guys!

I would like to create a text box that will auto complete the field name ContractNo from my database without using ajax. Any help?
here is what i tried:
In my .ASPX
XML
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<head>
<title>AutoComplete Textbox with webservice using jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
   SearchText();
});
function SearchText() {
   $(".autosuggest").autocomplete({
      source: function (request, response) {
         $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "AutoComplete.asmx/GetAutoCompleteData",
            data: "{'username':'" + document.getElementById('contract_num').value + "'}",
            dataType: "json",
            success: function (data) {
               response(data.d);
            },
            error: function (result) {
               alert("Error");
            }
         });
      }
   });
}
</script>
</head>


I add a Web service Method:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

///

/// Summary description for AutoComplete
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 AutoComplete : System.Web.Services.WebService {

   public AutoComplete () {
   //Uncomment the following line if using designed components
   //InitializeComponent();
   }

   [WebMethod]
   public List GetAutoCompleteData(string username)
   {
      List result = new List();
      using (SqlConnection con = new SqlConnection("Data Source=192.168.100.252;Integrated Security=true;Initial Catalog=AMICASSA_Checklist"))
      {
         using (SqlCommand cmd = new SqlCommand("select DISTINCT Contract_num from rawdata where Contact_num LIKE '%'+@SearchText+'%'", con))
         {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText", username);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
               result.Add(dr["Contract_num"].ToString());
            }
            return result;
         }
      }
   }
}

But nothing Happens :(
Posted
Updated 1-Sep-14 22:53pm
v6
Comments
Nelek 1-Sep-14 5:28am    
DarkDreamer08 1-Sep-14 20:58pm    
here is what i tried:
In my .ASPX
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<head>
<title>AutoComplete Textbox with webservice using jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "AutoComplete.asmx/GetAutoCompleteData",
data: "{'username':'" + document.getElementById('contract_num').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
</script>
</head>


I add a Web service Method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

///
/// Summary description for AutoComplete
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 AutoComplete : System.Web.Services.WebService {

public AutoComplete () {

//Uncomment the following line if using designed components
//InitializeComponent();
}


[WebMethod]
public List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=192.168.100.252;Integrated Security=true;Initial Catalog=AMICASSA_Checklist"))
{
using (SqlCommand cmd = new SqlCommand("select DISTINCT Contract_num from rawdata where Contact_num LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["Contract_num"].ToString());
}
return result;
}
}
}

}

But nothing Happens :(
Why without using Ajax? What is the issue? You can easily do this using jQuery or Ajax Control Toolkit.
DarkDreamer08 2-Sep-14 3:09am    
I do not know on how to use AJAX. I am not that good at jQuery. I am really sorry ! :(
DarkDreamer08 2-Sep-14 21:53pm    
How to use Ajax Control tool kit? should I download it?

You could, you know, search[^] before you ask.
 
Share this answer
 
 
Share this answer
 
Comments
DarkDreamer08 15-Sep-14 21:33pm    
Please Help me here :) http://www.codeproject.com/Questions/817498/How-to-Copy-From-Excel-and-Paste-it-into-a-Grid-Vi
You Can use ajax toolkit to solve auto complate probleam
easilly download free and latest version is 4.5
i recommended you to use 4.0 version.
 
Share this answer
 

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