Hello,
I'm a fairly new programmer and very new to web programming and I need some ASP.NET remediation. I'm trying to implement an autocomplete control in a form. When I put the search logic in the form codebehind, everything works. However, once I move the search logic to a web service (the autocomplete is still calling the same method in the form code), everything goes screwy. I'm using an OleDb connection and trying to access a test db on my workstation, but I get an error saying that the server is inaccessible.
Obviously, I could stick with the aspx codebehind, but regardless I need to understand the basic workings of a web service. Can someone help me understand where I'm making the error?
What I have looks like this:
Form.aspx.cs
...
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetContacts(string prefixText, int count)
{
AutoCompleteService autoCompleteService = new AutoCompleteService();
autoCompleteService.QueryString = "SELECT ...";
autoCompleteService.ConnectionString = UserContext.ConnectionString;
return autoCompleteService.GetResults(prefixText, count);
}
...
AutoComplete.Service.asmx
<%@ WebService Language="C#" CodeBehind="AutoComplete.Service.cs" Class="AutoCompleteService" %>
AutoComplete.Service.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Data;
[WebService(Namespace = "http://tempuri.org/")]
[System.Web.Script.Services.ScriptService]
public class AutoCompleteService : System.Web.Services.WebService {
public string QueryString;
public string ConnectionString;
public AutoCompleteService()
{
QueryString = "";
ConnectionString = "";
}
[WebMethod]
public String[] GetResults(string prefixText, int count)
{
...
}
private String[] GetNameListFromDB()
{
List<string> resultList = new List<string>();
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open(); //Exception thrown here.
...
}
Exception
ServerVersion = 'connection.ServerVersion' threw an exception of type 'System.InvalidOperationException'