Web方法Web服务没有返回数据

时间:2013-10-09 03:29:25

标签: c# sql

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

 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();
  } 

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

  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>");

        }
    }

帮助!它不返回分支机构名称,区号,区域代码,公司代码和公司名称?

1 个答案:

答案 0 :(得分: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;
}

由于您的方法签名随上面的实现而改变,如果返回null对象,您可以将其视为当前实现的false情况,如果对象从服务返回,您可以将其视为true案件。

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

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

然后您可以从服务方法返回上方。从客户端,您有Boolean值和AccessRequest