Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I implemented an autocomplete textbox using following code and it works fine :

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AutoComplete2._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetCompletionList(string prefixText, int count)
{
    return "this is sample text".Split();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
            ServiceMethod="GetCompletionList"
            CompletionInterval="500"
            ServicePath="" TargetControlID="TextBox1"
            EnableCaching="true"
            CompletionSetCount="20"
            DelimiterCharacters=";, :"
            UseContextKey="True">
        </asp:AutoCompleteExtender>
        <br />
    </div>
    </form>
</body>
</html>


I want the autocomplete to read from database. And i tried using the following code in above script, but it didn't work and shows some error
in "SqlConnection oConn = new SqlConnection(connect);" part:

XML
<script>
        public string[] GetCompanyName(string prefixText, int count)
        {
            string sql = "Select * from Table1 Where Company_Name like '" + prefixText + "%" + "'";
            string connect = ConfigurationManager.AppSettings["Conn"];
            SqlConnection oConn = new SqlConnection(connect);
            SqlDataAdapter da = new SqlDataAdapter(sql, oConn);
            da.SelectCommand.Parameters.Add(@prefixText, SqlDbType.NVarChar, 50).Value = prefixText + "%";
            DataTable dt = new DataTable();
            da.Fill(dt);
            string[] items = new string[dt.Rows.Count];
            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
                items.SetValue(dr["Company_Name"].ToString(), i);
                i++;
            }
            return items;
        }
</script>


could anyone tell me where's the wrong? Is my code above correct?
Posted
Updated 15-Aug-10 19:35pm
v2
Comments
Kunal Chowdhury «IN» 16-Aug-10 1:36am    
Reason for Edit: Proper code formatting & remove grammatical errors.

Assuming that, the connection string is properly configured in the configuration manager (In Web.config) and the following code returns a valid connection string,

string connect = ConfigurationManager.AppSettings["Conn"];


You need to open the connection using the following code:

C#
try
{
    oConn.Open();
}
catch (Exception ex)
{
    //You can log your exception message here
}
finally
{
  // Closing sql Connection
  oConn.Close();
}


If the connection is not opened, the DataAdapter (SqlDataAdapter) won't be able to fetch data from the database. Hopefully, this will solve the problem. :)
 
Share this answer
 
v2
Comments
raju melveetilpurayil 16-Aug-10 5:42am    
[edit]Sql Connection is not Closed.[/edit]
Al-Farooque Shubho 16-Aug-10 8:32am    
Yes of course. You need to close the connection once you are done with your database operation, always. So obvious that, didn't bother to mention. Thanks.
Hi,thanks friend.
I have solved the problem.
I added following code on aspx file:
<code>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %></code>





And this code on cs file:
using System.Data.SqlClient;


it works fine,it reads and suggests from database.

The complete code will look like this :
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AutoComplete2._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
    public static string[] GetCompletionList(string prefixText, int count)
    {
        string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection connection = new SqlConnection(conString);
        connection.Open();
        SqlParameter prm;
        string sql = "Select Company_Name FROM Table1 WHERE Company_Name LIKE @prefixText";
        SqlDataAdapter cmd = new SqlDataAdapter(sql, connection);
        prm = new SqlParameter("@prefixText", SqlDbType.VarChar, 50);
        prm.Value = prefixText + "%";
        cmd.SelectCommand.Parameters.Add(prm);
        DataTable dt = new DataTable();
        cmd.Fill(dt);
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr["Company_Name"].ToString(), i);
            i++;
        }
        connection.Close();
        return items;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <asp:TextBox ID="TextBox1" runat="server" Height="22px" Width="252px" ></asp:TextBox>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
            ServiceMethod="GetCompletionList"
            CompletionInterval="500"
            ServicePath="" TargetControlID="TextBox1"
            EnableCaching="true"
            CompletionSetCount="20"
            DelimiterCharacters=";, :"
            UseContextKey="True">
        </asp:AutoCompleteExtender>
        <br />
    </div>
    </form>
</body>
</html>
 
Share this answer
 
v2
Comments
raju melveetilpurayil 16-Aug-10 5:49am    
add comment where you made update the above code.

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