Click here to Skip to main content
6,595,444 members and growing! (16,786 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General License: The Code Project Open License (CPOL)

AutoCompelete Control Extender with With DB

By Mohammad Rizwan

AutoCompelete Control Extender with With DB
Javascript, CSS, HTML, ASP, ASP.NET, Ajax
Posted:30 Mar 2008
Views:5,075
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
1 vote for this article.
Popularity: 0.00 Rating: 1.00 out of 5
1 vote, 100.0%
1

2

3

4

5

Introduction

autoComplete Control Extender of AjaxToolKit with Database Interecation

Background

Background of this Article is using webservice for calling Database Hit

Using the code

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

Blocks of code should be set as style "Formatted" like this:

 
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoComplete.aspx.cs" Inherits="AutoComplete" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <table border="0" cellpadding="0" cellspacing="0" style="width: 80%" align="center">
            <tr>
                <td style="width: 100px">
                     
                </td>
                <td style="width: 100px">
                     
                </td>
                <td style="width: 100px">
                     
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                     
                </td>
                <td style="width: 100px">
                    <asp:TextBox runat="server" ID="myTextBox" Width="300" autocomplete="off" />
                    <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="myTextBox"
                        ServicePath="AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
                        CompletionInterval="200" EnableCaching="true" CompletionSetCount="12" />
                </td>
                <td style="width: 100px">
                     
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                     
                </td>
                <td style="width: 100px">
                     
                </td>
                <td style="width: 100px">
                     
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

<script type="text/javascript">
// Work around browser behavior of "auto-submitting" simple forms
var frm = document.getElementById("aspnetForm");
if (frm) {
           frm.onsubmit = function() { return false; };
      }
</script>

 
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; 

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : WebService
{
    string connectionString = string.Empty;
    DataTable dt = null;
    SqlCommand objCommnad = null;
    SqlDataAdapter da = null;
    SqlConnection con = null;
    public AutoComplete()
    {
    }

    [WebMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        if (count == 0)
        {
            count = 10;
        }

        if (prefixText.Equals("xyz"))
        {
            return new string[0];
        }

        Random random = new Random();
        List<string> items = new List<string>(count);
        for (int i = 0; i < count; i++)
        {
            char c1 = (char)random.Next(65, 90);
            char c2 = (char)random.Next(97, 122);
            char c3 = (char)random.Next(97, 122);

            items.Add(prefixText + c1 + c2 + c3);
        }

        //return items.ToArray();

        return getCompany(prefixText); 

        /////////////////Rizwan Code Here \\\\\\\\\\\\\\\\\\\\\\\

    }
    private string[] getCompany(string CompanyNameLike)
    {
        connectionString = ConfigurationManager.AppSettings["ConStr1"].ToString();
        con = new SqlConnection(connectionString);
        try
        {
            con.Open();
            objCommnad = new SqlCommand();
            objCommnad.Connection = con;
            objCommnad.CommandType = CommandType.StoredProcedure;
            objCommnad.CommandText = "LU_Select_ProPerty";
            objCommnad.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = CompanyNameLike;
            da = new SqlDataAdapter(objCommnad);
            dt = new DataTable();
            da.Fill(dt);
            //return dt;
        }
        catch (Exception ex)
        {
            con.Close();
            throw ex;
        }
        finally
        {
            con.Close();
        }
        List<string> items = new List<string>(dt.Rows.Count);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            items.Add(dt.Rows[i]["pro_name"].ToString());     
        }
        return items.ToArray() ;
    }
}


        

Remember to set the Language of your code snippet using the Language dropdown.

Use the "var" button to to wrap Variable or class names in &lt;code&gt; tags like this.

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

Keep a running update of any changes or improvements you've made here.

License

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

About the Author

Mohammad Rizwan


Member
Sr Software Engineer
Axact Pvt Ltd
Karachi Pakistan
Occupation: Software Developer (Senior)
Company: Axact Pvt Ltd
Location: Pakistan Pakistan

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralThis control having prob with IE6 PinmemberSuresh gudiseva0:49 18 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Mar 2008
Editor:
Copyright 2008 by Mohammad Rizwan
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project