Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

AutoCompleteExtender: Need to use the connection string in Web.config to populate data for GetCompletionList

$
0
0

Hey guys,

I've got a project with several text boxes for searching client names from a database. I want to use the AutoCompleteExtender control to populate search guess as the user types.

I have the script manager in place as well as my AutoCompleteExtender. I have also set a page method, as to configure data for the results list. I am stuck here. I have set an SQL query to get data from a table and a colum within the table. Somehow, I dont think I should be using the System.Data.SqlClient; namespace. I am using Sql compact edition and I already have a ConnectionString in my Web.config file.

How can I utilize my existing connection string(web.config), rather than using System.Data.SqlClient;? Or if I use a connection manager, how can I set it to look at my compact edition database?

ASP:

<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
        Remote Administration</h1><strong><p>Client Search:</p></strong><asp:TextBox ID="ClientSearch" runat="Server" Width="227px"></asp:TextBox><ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" TargetControlID="ClientSearch" runat="server" ServiceMethod="GetCompletionList" UseContextKey="True"></ajaxToolkit:AutoCompleteExtender><asp:Button ID="SearchSubmit" runat="Server" Text="Search" />

Code Behind (C#):

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString; 
        SqlConnection conn = new SqlConnection(connectionString); 
        SqlCommand cmd = new SqlCommand("SELECT TOP " + count + " columnname FROM tablename WHERE columnname LIKE '" + prefixText + "%'", conn); 
        SqlDataReader oReader; conn.Open(); 
        List<string> CompletionSet = new List<string>(); 
        oReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
        while (oReader.Read()) CompletionSet.Add(oReader["columnname"].ToString()); 
        return CompletionSet.ToArray();
    }
}

Web.config:

<connectionStrings><add name="ConnectionString" connectionString="Data Source=|DataDirectory|\Clients.sdf" providerName="System.Data.SqlServerCe.4.0" /><add name="ClientsEntities" connectionString="metadata=res://*/App_Code.Model.csdl|res://*/App_Code.Model.ssdl|res://*/App_Code.Model.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;data source=|DataDirectory|\Clients.sdf&quot;" providerName="System.Data.EntityClient" /></connectionStrings>





Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>