AutoCompleteExtender without webservice

AutoCompleteExtender without webservice



Here is the HTML portion




<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Country Name :-
<asp:TextBox ID="txtKey" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtenderDemo" runat="server"
TargetControlID="txtKey" ServiceMethod="GetCompletionList"
MinimumPrefixLength="1" CompletionInterval="1000"
EnableCaching="true" CompletionSetCount="20">
</cc1:AutoCompleteExtender>
</div>
</form>


See code behind functions





#region GetCompletionList
/// <summary>
/// GetCompletionList
/// </summary>
/// <param name="prefixText">search text</param>
/// <param name="count">no of matches to display</param>
/// <returns>string[] of matching names</returns>
[System.Web.Services.WebMethod]
public static string[] GetCompletionList(String prefixText, int count)
{
List<String> suggetions = GetSuggestions(prefixText, count);
return suggetions.ToArray();
}
#endregion


Creating Sql connection object





#region GetSqlConnection
/// <summary>
/// GetSqlConnection
/// </summary>
/// <returns>SqlConnection</returns>
private static SqlConnection GetSqlConnection()
{
string connString =
ConfigurationManager.ConnectionStrings["CONNSTRING"].ConnectionString;
return new SqlConnection(connString);
}
#endregion


Creating Sql command object





#region GetSqlCommand
/// <summary>
/// GetSqlCommand
/// </summary>
/// <param name="conn">SqlConnection</param>
/// <param name="key">Key</param>
/// <returns>SqlCommand</returns>
private static SqlCommand GetSqlCommand(SqlConnection conn, string key, int count)
{
string commandText =
"SELECT TOP " + count.ToString() + " CountryName FROM Countries WHERE CountryName LIKE '" + key + "%'";
SqlCommand comm = new SqlCommand(commandText, conn);
comm.CommandType = CommandType.Text;
return comm;
}
#endregion


This function is to Fetch matching country names from DB





#region GetSuggestions
/// <summary>
/// GetSuggestions
/// </summary>
/// <param name="key">Country Names to search</param>
/// <returns>Country Names Similar to key</returns>
private static List<String> GetSuggestions(string key, int count)
{
List<String> suggestions = new List<string>();
DataTable dtSuggestions = new DataTable();

using (SqlConnection conn = GetSqlConnection())
{
conn.Open();

using (SqlCommand comm = GetSqlCommand(conn, key, count))
{
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dtSuggestions);
}
}

if (dtSuggestions != null
&& dtSuggestions.Rows != null
&& dtSuggestions.Rows.Count > 0)
{
foreach (DataRow dr in dtSuggestions.Rows)
{
string suggestion = dr["CountryName"].ToString();
suggestions.Add(suggestion);
}
}

return suggestions;
}
#endregion


Friends here i attached a copy of the working project.


Attachments

  • AutoCompleteExtender without webservice Sample (30824-12436-AutoCompleteSample.zip)
  • AutoCompleteExtender without webservice Sample (30824-1252-AutoCompleteSample.zip)
  • Comments

    Author: Mohammad Vahaj uddin05 Aug 2009 Member Level: Silver   Points : 1

    Im using your code but the functions where to call your functions. I've exactly copied and paste your code in my application its not giving any kind of errors neither its generating some kind of output.

    Author: Mohammad Vahaj uddin05 Aug 2009 Member Level: Silver   Points : 1

    Im using your code but the functions where to call your functions. I've exactly copied and paste your code in my application its not giving any kind of errors neither its generating some kind of output.

    Author: guru prasad11 Aug 2009 Member Level: Gold   Points : 1

    Your code is not working .......


    can you attach the files which are working........


    Other wise correct the code.....


    Thanks & Regards
    Guru Prasad.

    Author: Mohammad Vahaj uddin12 Aug 2009 Member Level: Silver   Points : 0

    My backend is oracle is it Ok that im using your code with oracleclient.

    Author: Mohammad Vahaj uddin12 Aug 2009 Member Level: Silver   Points : 0

    Author: Mohammad Vahaj uddin12 Aug 2009 Member Level: Silver   Points : 0

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.OracleClient;
    using System.Collections;
    using System.Web.Services;
    using System.Configuration;

    public partial class sugg : System.Web.UI.Page
    {



    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region GetCompletionList
    ///


    /// GetCompletionList
    /// ///

    /// /// search text
    /// /// no of matches to display
    /// string[] of matching names
    /// [System.Web.Services.WebMethod]
    public static string[] GetCompletionList(String prefixText, int count)
    {
    List suggetions = GetSuggestions(prefixText, count);
    return suggetions.ToArray();
    }
    #endregion


    #region GetSqlConnection
    ///
    /// /// GetSqlConnection
    ///

    /// SqlConnection
    private static OracleConnection GetSqlConnection()
    {
    string connString = ConfigurationManager.ConnectionStrings["DataConvConnectionString_gisad"].ConnectionString;
    return new OracleConnection(connString);
    }
    #endregion


    private static OracleCommand GetSqlCommand(OracleConnection conn, string key, int count)
    {
    string commandText = "select EMP_NAME from employees_details where EMP_NAME like '" + key + "%'";
    OracleCommand comm = new OracleCommand(commandText, conn);
    comm.CommandType = CommandType.Text;
    return comm;
    }

    private static List GetSuggestions(string key, int count)
    {
    List suggestions = new List();
    DataTable dtSuggestions = new DataTable();
    using (OracleConnection conn = GetSqlConnection())
    {
    conn.Open();
    using (OracleCommand comm = GetSqlCommand(conn, key, count))
    {
    OracleDataAdapter adptr = new OracleDataAdapter(comm); adptr.Fill(dtSuggestions);
    }
    }
    if (dtSuggestions != null && dtSuggestions.Rows != null && dtSuggestions.Rows.Count > 0)
    {
    foreach (DataRow dr in dtSuggestions.Rows)
    {
    string suggestion = dr["EMP_NAME"].ToString();
    suggestions.Add(suggestion);
    }
    } return suggestions;
    }

    }

    Author: Sabu C Alex12 Aug 2009 Member Level: Gold   Points : 1

    hi friends
    here i attached a copy of the working project . Pls download and try that

    Author: Sabu C Alex12 Aug 2009 Member Level: Gold   Points : 2

    hi dear Mohammad Vahaj uddin

    This is an example for Using Autocompleteextender without webservice.

    For that we dont want to use the "ServicePath" attribute.
    In your code you gave that also. So that will search for a webservice method.

    ServicePath ="~/App_Code/WebService.cs"

    I think you got the point.
    Otherwise you download the sample project i attached with this resource.

    I HOPE THIS WILL HELP YOU

    Sabu C Alex

    Author: Mohammad Vahaj uddin12 Aug 2009 Member Level: Silver   Points : 0

    Thanx a lot Sabu u really solved my problem thanx a lot.

    Author: Mohammad Vahaj uddin12 Aug 2009 Member Level: Silver   Points : 0

    Thanx a lot Sabu u really solved my problem thanx a lot.

    Author: Abhay Kumar Raut13 Aug 2009 Member Level: Gold   Points : 1

    Hi Sabu C Alex,

    good one .

    keep it up.

    contribute more.

    Thanks and Regards
    Abhay

    Author: Abhi10 Mar 2011 Member Level: Bronze   Points : 1

    Hi,
    I am using your code and i am able to get the result,
    But the column from where i intend to search contains paragrphs,
    So in suggestion box its returning me entire para starting with the letters i insert.I tried using delimiter property also but still the prob remains.I just want 1st line of the para to be returned.How do i do it?

    Author: Abhi11 Mar 2011 Member Level: Bronze   Points : 0

    Hi I got the solution...

    Guest Author: B Smadi13 Jun 2012

    thank you a million



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: