LINQ - Autocomplete Extender - Asked By svm satya on 05-Jan-12 02:04 AM

Hi,


               i want autocomplete extender functionality  to search for different tables columns  using linq to sql.

            can any body help me.my mail id svmbabu547@gmail.com.

 
Riley K replied to svm satya on 05-Jan-12 02:07 AM


You can create a webmethod that queries and retruns list

[WebMethod]
public List<Employee> FetchEmailList(string mail)
{
  var emp = new Employee();
  var fetchEmail = emp.GetEmployeeList()
  .Where(m => m.Email.ToLower().StartsWith(mail.ToLower()));
  return fetchEmail.ToList();
}
the below is jQuery autocomplete extender

<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">
$(function() {
  $(".tb").autocomplete({
    source: function(request, response) {
      $.ajax({
        url: "EmployeeList.asmx/FetchEmailList",
        data: "{ 'mail': '" + request.term + "' }",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function(data) { return data; },
        success: function(data) {
          response($.map(data.d, function(item) {
            return {
              value: item.Email
            }
          }))
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    },
    minLength: 2
  });
});


Regards
Reena Jain replied to svm satya on 05-Jan-12 02:32 AM
HI,

If you have recently used Google's web search, then you have seen this functionality first hand. When you start typing a search term, Google will show you a list of common searches from their database that are prefixed with what you have typed.

Please note that this behavior should not be confused with a web browser's autocomplete history, which displays a list of recent items that you have personally entered into text boxes. A browser's autocomplete history is generated from the local machine, unlike ASP.NET autocomplete which is generated by the server.

The ASP.NET autocomplete functionality is provided by an ASP.NET AJAX Extender, which you can attach to any ASP.NET textbox control on your web page. When a user begins to type into the textbox, the extender will query either a web service or a page method and return items that are prefixed with what the user has currently entered, which are then displayed in a panel below the text box for easy selection and reference.

The ServiceMethod references the page method that will return results to display, as we define below.

<asp:TextBox runat="server" ID="txtAutoComplete" />
 
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtAutoComplete" ServiceMethod="GetCompletionList" />

To make this easy we want to use a page method to return results instead of building a web service. This method would go in the code behind the page that contains the above textbox and extender, and must have a very specific signature so that it can be called by the ASP.NET autocomplete extender. Here is a brief example that returns a static list of items:

[System.Web.Services.WebMethod]
 
[System.Web.Script.Services.ScriptMethod]
 
public static string[] GetCompletionList(string prefixText, int count)
 
{
 
//Place your code to create a string array here. It should query some data using the prefixText and count
 
string[] list = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"};
 
return list;
 
}
dipa ahuja replied to svm satya on 05-Jan-12 02:52 AM
Step 1 : Take a TextBox , AutoCompleteExtender
 
<asp:TextBox runat="server" ID="txtInput" Width="300" />
<cc1:AutoCompleteExtender runat="server" ID="AutoComplete1"
TargetControlID="txtInput"
ServiceMethod="GetNames"
ServicePath="WebService.asmx"
MinimumPrefixLength="1"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="20"/>
 
Step 2 : Add ScriptManager on page with <Services> tag
 
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
  <asp:ServiceReference Path="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
 
Step 3: Add a Webservice file in your website by Right click on website->Add new Item ->Web Service and add this code inside it..
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
 
public class WebService : System.Web.Services.WebService
{
  public WebService()
  {
  }
 
  [WebMethod]
  public string[] GetNames(string prefixText, int count)
  {
    List<string> items = new List<string>(count);
    DataSet ds = new DataSet();
 
    string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(cs))
    {
        string sql = "SELECT name FROM people WHERE name LIKE '" + prefixText + "%'";
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = new SqlCommand(sql, connection);
      adapter.Fill(ds);
    }
 
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
      items.Add(dr["name"].ToString());
    }
 
    return items.ToArray();
  }
}
 
 
svm satya replied to dipa ahuja on 05-Jan-12 03:30 AM
Hi,

       i want to write code for search using auto complete extender for two tables fields using linq to sql.
svm satya replied to dipa ahuja on 05-Jan-12 03:30 AM
Hi,

       i want to write code for search using auto complete extender for two tables fields using linq to sql.
svm satya replied to dipa ahuja on 05-Jan-12 03:31 AM
Hi,

       i want to write code for search using auto complete extender for two tables fields using linq to sql.
Autocomplete Extender - svm satya replied to dipa ahuja on 05-Jan-12 03:32 AM
Hi,

       i want to write code for search using auto complete extender for two tables fields using linq to sql.
svm satya replied to Riley K on 05-Jan-12 03:37 AM
Thank you very much.but i want two tables.
Riley K replied to svm satya on 05-Jan-12 03:42 AM



You can use the join keyword to get the desired results

eg

var query =
  from department in departments
  join employee in employees
    on department.ID equals employee.DepartmentID
    into employeeGroups
  from employee in employeeGroups.DefaultIfEmpty()
  select new { DepartmentName = department.Name, EmployeeName = employee.Name };


Regards
svm satya replied to Riley K on 05-Jan-12 03:47 AM
how can i bind it to one text box.
Riley K replied to svm satya on 05-Jan-12 03:51 AM


Write that method in webmethod and return as list
Autocomplete Extender - svm satya replied to Riley K on 05-Jan-12 03:53 AM
 Thank you very much.
Riley K replied to svm satya on 05-Jan-12 04:15 AM

Welcome