请问如何解决error: identifier out of scope问题?
我现在有一个ReturnStockHolding的web services,然后我想在一个web application里调用它,可它给了个error: identifier 'ReturnStockHolding' out of scope给我,请问我应该如何解决?ReturnStockHolding本身没有问题,可以返回数值。
StockHoldingDetail.asmx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using DodoBusinessLib;
namespace StockHoldingDetail
{
/// <summary>
/// Summary description for Service1.
/// </summary>
[WebService(Namespace="http://localhost/StockHolding/")]
public class StockHoldingDetail : System.Web.Services.WebService
{
public StockHoldingDetail()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5
private OnlineApplication online = new OnlineApplication();
private DataSet stock = new DataSet();
/// <summary>
/// Return Stock Holding Details.
/// </summary>
[WebMethod]
public int ReturnStockHolding(int id, string password, int productid, string database)
{
online.SetDatabase(database);
if ((online.ValidateUser(id, password) && (online.ValidateProduct(productid))))
{
stock = online.SetStockHolding(productid);
return int.Parse(stock.Tables["product"].Rows[0]["qty_in_stock"].ToString());
}
else
return -1;
}
}
}
StockChecking.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DodoBusinessLib;
namespace StockHoldingWebApp
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnSubmit;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.TextBox txtUsername;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox txtProduct;
protected System.Web.UI.WebControls.TextBox txtDatabase;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label lblError;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSubmit_Click(object sender, System.EventArgs e)
{
localhost.StockHoldingDetail shd = new StockHoldingWebApp.localhost.StockHoldingDetail();
int userid = int.Parse(txtUsername.Text);
string password = txtPassword.Text;
int product = int.Parse(txtProduct.Text);
string database = txtDatabase.Text;
int result = 0;
try
{
if (txtUsername.Text!="" && txtPassword.Text!="" && txtProduct.Text!="" && txtDatabase.Text!="")
{
result = shd.ReturnStockHolding(userid, password, product, database);
lblError.Text = result.ToString();
}
else
{
lblError.Text = "Your username or password is not correct, please enter again";
}
}
catch
{
lblError.Text = "Your database is not correctly set, please set it again";
}
}
}
}
当我把try catch拿掉后,我又有以下的错误:
Server Error in '/StockHoldingWebApp' Application.
--------------------------------------------------------------------------------
The request failed with HTTP status 401: Access Denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The request failed with HTTP status 401: Access Denied.
Source Error:
Line 35: [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost/StockHolding/ReturnStockHolding", RequestNamespace="http://localhost/StockHolding/", ResponseNamespace="http://localhost/StockHolding/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
Line 36: public int ReturnStockHolding(int id, string password, int productid, string database) {
Line 37: object[] results = this.Invoke("ReturnStockHolding", new object[] {
Line 38: id,
Line 39: password,
Source File: c:\inetpub\wwwroot\StockHoldingWebApp\Web References\localhost\Reference.cs Line: 37
Stack Trace:
[WebException: The request failed with HTTP status 401: Access Denied.]
System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
StockHoldingWebApp.localhost.StockHoldingDetail.ReturnStockHolding(Int32 id, String password, Int32 productid, String database) in c:\inetpub\wwwroot\StockHoldingWebApp\Web References\localhost\Reference.cs:37
StockHoldingWebApp.WebForm1.btnSubmit_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\stockholdingwebapp\stockchecking.aspx.cs:71
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()