views:

37

answers:

1

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'
+1  A: 

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.

cRichter
Thanks for the response. I know the service is getting used as the code throws the exception in the service code. I don't understand why I can make a connection when the logic is in codebehind, but not in the service.In regards to MVC: I've looked at it, but was given the impression that it's designed for large-scale projects. I work on a two man team.
Erasmus777
cRichter
Thanks for all the help. It turns out that there was a small typo that was causing the problem. Grrr. Regardless, I learned a lot about web services :)
Erasmus777