简体   繁体   English

无法按照教程创建动态搜索结果

[英]Unable to follow tutorial to create dynamic search results

I am following this tutorial to create dynamic search results from an SQL server as a user types. 我正在按照教程的操作,根据用户类型从SQL Server创建动态搜索结果。 It is telling me to create a .asmx file, which is not a format I have ever worked with before. 它告诉我创建一个.asmx文件,该文件不是我以前使用过的格式。 Here is the code I have thus far : 这是我到目前为止的代码:

WebService.asmx.cs : 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.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 编译器错误消息: BC30689:语句不能出现在方法主体之外

Your web methods must be declared inside a class, and the attributes must decorate that class. 您的Web方法必须在一个类中声明,并且属性必须装饰该类。

[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
public class SearchService : WebService
{
  [WebMethod]
  public searchResult[] Search(string txtSearch)
  {
     // ...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM