繁体   English   中英

非静态字段方法或属性需要对象引用

[英]an object reference is required for the non-static field method or property

我正在尝试在Sql查询中使用文本框ID,但显示上面的错误。 当我提供客户名称而不是客户ID时,它可以正常工作。

这是我的代码。

  [System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]

public static List<string> SearchAddress(string prefixText, int count)
{

   using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = dbConnection.fnConnectionString();
        using (SqlCommand cmd = new SqlCommand())
        {             
            cmd.Connection = conn;

            conn.Open();

            cmd.CommandText = @"select city,Addresscode from BName_Addresscode
                           where Customer='"+txtCustomerName.text+'";

            List<string> customers1 = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {

                    String Code = sdr["City"].ToString();
                    String Name = sdr["Addresscode"].ToString();
                    Name = Code + "(" + Name + ")";
                    customers1.Add(Name);

                }
            }
            conn.Close();
            return customers1;
        }
    }
}

那行不通,这就是原因。

您的页面方法是静态的

public static List<string> SearchAddress(string prefixText, int count)

这意味着您不能引用实例成员, txtCustomerName.Text引用您的类的实例成员( txtCustomerName )。

要解决此问题,请将实际的文本框值移至此方法的参数 ,有趣的事实是您可能已经在其中了, prefixText参数似乎是一个,不是吗?

public static List<string> SearchAddress(string **prefixText**, int count)

然后,在您的JavaScript中,只需将值传递给docs中所述的方法即可:

function GetSearchAddress() 
{
    var customer = $get("txtCustomerName").value;
    var count    = ...;
    PageMethods.SearchAddress(customer, count, onCompleted);
}

暂无
暂无

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

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