Autocomplete from database
Hi..
I implement an autocomplete texttbox using following code and it works fine :
[code]
<pre lang="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></pre>
[/code]
I want the autocomplete to read from database. And i tried used following code in above script,but it doesn't work and shows some error
in "SqlConnection oConn = new SqlConnection(connect);" part:
[code]
<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>
[/code]
could anyone tell me where's the wrong?Is my code above correct?