0

I am following this tutorial to create dynamic search results from an SQL server as a user types. It is telling me to create a .asmx file, which is not a format I have ever worked with before. Here is the code I have thus far :

WebService.asmx.cs :

public class SearchService : WebService
{
  [WebMethod]
  public searchResult[] Search(string txtSearch)
  {
//Declare collection of searchResult
        List resultList = new List();
        var db = Database.Open("mPlan");
        var result = db.Query("SELECT * from Users where Username like '%" + txtSearch + "%'");
       try
       {
           foreach(var record in result)
            {
               searchResult result = new searchResult();
               result.Username = ["Username"].ToString();
               resultList.Add(result);
           }
           return resultList.ToArray();
       }
       catch
       {
           return null;
       }
  }}

WebService.asmx :

<%@ WebService Language="C#" class="WebService.asmx.cs" %>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
public class searchResult
{
    public string Title;
    public string img;
    public string href;
}

Here is my error message, can anyone help me with this please?

Compiler Error Message: BC30689: Statement cannot appear outside of a method body

Simon Kiely
  • 5,880
  • 28
  • 94
  • 180
  • Well, what are those 2 lines meant to be doing? when are they meant to be executed? – Marc Gravell Mar 12 '12 at 13:28
  • You're missing a `namespace` declaration – Morphed Mar 12 '12 at 13:29
  • Slightly off topic, but you should really try and prevent SQL injection, check out this link: http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html – Matthew Mar 12 '12 at 13:31
  • @MarcGravell the class is connecting to the database and returning the information. I believe the two methods that are not working should be exposing this data to the JQuery in my webpage that calls it when a user types. – Simon Kiely Mar 12 '12 at 13:37
  • ah, they've suddenly become attributes rather than free-floating statements; that looks like a step in the right direction – Marc Gravell Mar 12 '12 at 13:39
  • @MarcGravell, thank you. I am now getting the error Compiler Error Message:BC30689: Statement cannot appear outside of a method body/multiline lambda. Line 2: using System;, however. Any ideas? – Simon Kiely Mar 12 '12 at 13:49
  • 1
    @Simon it has been a while, but presumably there's a different syntax for including that on a service page? something involving bee-stings, no doubt – Marc Gravell Mar 12 '12 at 13:52

1 Answers1

0

Your web methods must be declared inside a class, and the attributes must decorate that class.

[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
public class SearchService : WebService
{
  [WebMethod]
  public searchResult[] Search(string txtSearch)
  {
     // ...
  }
}
jrummell
  • 42,637
  • 17
  • 112
  • 171
  • I have now updated my code to reflect your changes, however it is still returning the same error when I attempt to run it. Any ideas? – Simon Kiely Mar 12 '12 at 13:35
  • Compiler Error Message:BC30689: Statement cannot appear outside of a method body/multiline lambda. Line 2: using System; – Simon Kiely Mar 12 '12 at 13:48
  • I see the problem now. You are writing this code inside the .asmx file. After adding a new WebService via the Add New Item dialog, you should have a .asmx and a .asmx.cs file. Move all of the code to the .asmx.cs file, and make sure your class name matches the .asmx file name. E.g., if your filename is `SearchService.asmx`, your class should be `SearchService`. – jrummell Mar 12 '12 at 13:52
  • thank you very much for your response. It is exactly what I needed to know as a complete beginner! I have tried to add what you said (see code above) and it is returning the error BC30689: Statement cannot appear outside of a method body/multiline lambda. for the WebService.asmx/Search function. Any idea what might be causing this? – Simon Kiely Mar 12 '12 at 15:22
  • I have edited the code slightly and am now getting the error : Parser Error Message: Could not create type 'WebService.asmx.cs'! – Simon Kiely Mar 12 '12 at 15:44
  • That is a different question, however, I should post it in that. Thanks very much for your help. – Simon Kiely Mar 12 '12 at 15:51