1

我有一个搜索分支的方法。参数是分支代码,它应该返回分支的详细信息

 public bool SearchBranch()
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBReader"].ConnectionString))
        {

            using (SqlCommand com = new SqlCommand("Reader.usp_SearchBranch", con))
            {
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Add("@BranchCode", SqlDbType.Int).Value = this.BranchCode;

                con.Open();

                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    this.BranchName = dr.GetValue(0).ToString();
                    this.AreaCode = dr.GetValue(1).ToString();
                    this.RegionCode = dr.GetValue(2).ToString();
                    this.CompanyCode = dr.GetValue(3).ToString();
                    this.CompanyName = dr.GetValue(4).ToString();
                    return true;
                }
                else
                {
                    return false;
                }


            }
        }
    }

这是我在 Web 服务中的 Web 方法中的代码(我不知道这是否正确)

    [WebMethod(Description = "Search Affected User from Database in Access Request")]
     public bool SearchBranchAccessRequest(AccessRequest accessrequest)
  {
      return accessrequest.SearchBranch();
  } 

这就是我在我的网页中访问/调用 web 方法的方式

  protected void SearchBranchButton_Click(object sender, EventArgs e)
    {
        try
        {
            accessrequest.BranchCode = Convert.ToInt32(BranchCodeTextBox.Text);
            iTicketWebService.SearchBranchAccessRequest(accessrequest);

            if (iTicketWebService.SearchBranchAccessRequest(accessrequest) == true)
            {
                BranchNameLabel.Text = accessrequest.BranchName;
                AreaLabel.Text = accessrequest.AreaCode;
                RegionLabel.Text = accessrequest.RegionCode;
                CompanyCodeLabel.Text = accessrequest.CompanyCode;
                CompanyLabel.Text = accessrequest.CompanyName;
                BranchEmailLabel.Text = accessrequest.BranchCode + "@pjlhuillier.com";
            }
            else
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", "<script type=\"text/javascript\">alert('Record not found. Please try again');</script>");
            }
        }
        catch (Exception)
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", "<script type=\"text/javascript\">alert('Wrong Input. Please try again');</script>");

        }
    }

帮助!它不返回分行名称、区号、地区代码、公司代码和公司名称?

4

1 回答 1

1

如下更改您的网络方法

public AccessRequest SearchBranchAccessRequest(AccessRequest accessrequest)
{
    return accessrequest.SearchBranch(accessrequest);
} 

你也需要改变SearchBranch()方法

public accessrequest SearchBranch(AccessRequest accessrequest)
{

    if(you found record in database)
    {
       // update accessrequest here 
    }else
    {
      accessrequest =null;
    }
    // finally return the object
    return accessrequest;
}

当您调用此 Web 服务时

AccessRequest  request = iTicketWebService.SearchBranchAccessRequest(accessrequest);

if(request!=null)
{
    BranchNameLabel.Text = request.BranchName;
}

由于您的方法签名随上述实现而变化,因此在返回空对象的情况下,您可以将其视为false当前实现的案例,如果对象从服务返回,您可以将其视为true案例。

如果您需要true false从服务方法返回并且还需要更新对象,那么您可以使用自定义类来返回两者,如下所示

public class SearchBrancResponse
{
    public bool SearchStatus { get; set; }
    public AccessRequest  AccessReq  { get; set; }
}

然后,您可以从 service 方法返回上面。从客户端你Boolean有价值和AccessRequest

于 2013-10-09T03:38:51.190 回答