Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement a web service in ASP.NET
    primarykey
    data
    text
    <p>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.</p> <p>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?</p> <p>What I have looks like this:</p> <h2>Form.aspx.cs</h2> <pre><code> ... [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); } ... </code></pre> <h2>AutoComplete.Service.asmx</h2> <pre><code>&lt;%@ WebService Language="C#" CodeBehind="AutoComplete.Service.cs" Class="AutoCompleteService" %&gt; </code></pre> <h2>AutoComplete.Service.cs</h2> <pre><code>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&lt;string&gt; resultList = new List&lt;string&gt;(); OleDbConnection connection = new OleDbConnection(ConnectionString); connection.Open(); //Exception thrown here. ... } </code></pre> <h2>Exception</h2> <pre><code>ServerVersion = 'connection.ServerVersion' threw an exception of type 'System.InvalidOperationException' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload