Cody Hicks
Cody Hicks

Reputation: 420

Unable to populate data for my AutoCompleteExtender

I'm not sure the correct way to go about this. I've got a AutoCompleteExtender as well as the script manager in place for it. I am just unable to get the data from my database table to populate my AutoComplete. I've looked all over and I'm actually trying to get the data from my SQL compact table but no forum topics seem to relate specifically to how to connect to the SQL compact server..I've got a ConnectionString in place on my Web.config file and I would like to use that in the ConfigurationManager, in the C# code behind.

Any any case, here is my ASP:

    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" >
    </ajaxToolkit:ToolkitScriptManager>
    </asp:TextBox><ajaxToolkit:AutoCompleteExtender 
    ID="AutoCompleteExtender1" 
    TargetControlID="ClientSearch" 
    runat="server" 
    ServiceMethod="GetCompletionList" 
    MinimumPrefixLength="1" 
    UseContextKey="True">
   </ajaxToolkit:AutoCompleteExtender>
    <asp:Button ID="SearchSubmit" runat="Server" Text="Search" />

Here is my C# code behind:

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["ConnectionString"].ConnectionString; 
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SELECT TOP " + count + " Name FROM Current WHERE Name LIKE '" + prefixText + "%'", conn);
        SqlDataReader oReader; conn.Open(); List<string> CompletionSet = new List<string>();
        oReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (oReader.Read()) CompletionSet.Add(oReader["Name"].ToString()); return CompletionSet.ToArray();
    }
} 

Here is my Web.config ConnectionString:

    <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>

I'm most likely doing something wrong here. Could someone point me in the right direction?

Upvotes: 0

Views: 302

Answers (1)

Cody Hicks
Cody Hicks

Reputation: 26

Yuriy, you were right. I changed the types and name spaced to support SqlCe.

Garrison, You're probably right. If there is a better method, I'm open to suggestions. I was simply trying to use code from a tutorial. The application I'm building is going to be on a local intranet. I still wouldn't mind using best practice methods though.

I can use static string methods and store data within the strings but I'd rather be dynamic about it if I can help it.

Upvotes: 1

Related Questions