Reputation: 757
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:
...
[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);
}
...
<%@ WebService Language="C#" CodeBehind="AutoComplete.Service.cs" Class="AutoCompleteService" %>
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.
...
}
ServerVersion = 'connection.ServerVersion' threw an exception of type 'System.InvalidOperationException'
Upvotes: 1
Views: 1285
Reputation: 1411
You call the CodeBehind, and that just instantiates the class. it does not use the service.
with your client javascript library, you can call the service directly. without the code in codeBehind. but that depends on your clientside js library.
what i would recommend you is, get to lern MVC, or asp.net MVC, as they have such things much simpler, and with easier implemention in the end.
Upvotes: 1