2012-08-10 132 views
1

我已经创建在Visual Studio 2010.I一个Web服务创建了一个数据库,其中包含命令及其description.My计划表是: 我的类:-DataHelper.csweb服务在Visual Studio不工作2010

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data; 
using System.Data.SqlClient; 

/// <summary> 
/// Summary description for DataHelper 
/// </summary> 

    public class DataHelper 
    { 
     public static string GetData(string Command) 
     { 
      string Explanation = " "; 
      SqlConnection con = new SqlConnection(@"Data Source=CSS-L3-D008;Initial Catalog=works1;Integrated Security=true;"); 
      SqlCommand cmd = new SqlCommand("Select Explanation from Data1 where Command= '" + Command.ToUpper() + "'", con); 
      con.Open(); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      while (dr.Read()) 
      { 
       Explanation = dr["Explanation"].ToString(); 
      } 
      dr.Close(); 
      con.Close(); 
      return Explanation; 

     } 
    } 

而且Service1.asmx的

Imports System.Web.Services 
Imports System.Web.Services.Protocols 
Imports System.ComponentModel 

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
' <System.Web.Script.Services.ScriptService()> _ 
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ 
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<ToolboxItem(False)> _ 
Public Class Service1 
    Inherits System.Web.Services.WebService 

    Private Property DataHelper As Object 

    <WebMethod()> 
    Public Function HelloWorld() As String 
     Return "Hello World" 
    End Function 


    <WebMethod()> 
    Public Function GetData(Command) As String 

     Return DataHelper.GetData(Command) 
    End Function 
End Class 

问题是,文我运行它我得到双方的webMethods的HelloWorld和的GetData但的GetData不working.When我点击的HelloWorld()我得到调用方法和运行正确。但是当我点击GetData()我得到调用按钮埠那么它会显示 “测试表单仅适用于具有原始类型作为参数的方法。” 其实它应该给出一个框,我可以输入命令,描述应该从sql server返回。请帮助我。

+0

你看了消息的类型?它告诉你,你正在尝试的是不被支持的。它从来没有得到支持,它永远不会。您无法通过浏览器调用该方法。相反,编写一个客户端程序来调用它。此外,您应该停止使用ASMX Web服务,因为它们是传统技术,并不打算用于新开发。 – 2012-08-10 14:02:14

回答

1

应在服务,GetData方法声明命令参数?:

<WebMethod()> 
Public Function GetData(Command As String) As String 
    Return DataHelper.GetData(Command) 
End Function 
+0

雅我喜欢这一点,现在我能够运行它,但现在我调用我得到错误: – user1387035 2012-08-10 13:43:38

+0

System.NullReferenceException:对象变量或块变量未设置。 at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance,Type Type,String MemberName,Object [] Arguments,String [] ArgumentNames,Type [] TypeArguments,Boolean [] CopyBack) at MyService2.Service1.GetData(String Command)in c:\ documents and settings \ sczsc zsc zsc \ my documents \ visual studio 2010 \ Projects \ MyService2 \ MyService2 \ Service1.asmx – user1387035 2012-08-10 13:44:23