处理返回Null的XML Web服务方法

时间:2012-03-12 19:04:52

标签: c# web-services null

WebService.asmx.cs

[WebMethod]
public HashSet<callInfo> getCalls(string Date, string techID)
{
    HashSet<callInfo> list = new HashSet<callInfo>();
    var today = Convert.ToDateTime(Date);
    var tomorrow = today.AddDays(1).ToString("MM-dd-yyyy");
    var comm = new SqlCommand("Select RTrim(CallNbr), RTrim(CustNmbr), RTrim(AdrsCode), RTrim(SvcDescr), RTrim(SrvStat), RTrim(Customer_Reference), Convert(Varchar, ETADTE, 101), Convert(Varchar, ETATME, 108), RTrim(Address1), RTrim(City), RTrim(State), RTrim(Zip), Left(Phone1, 3) + '-' + SubString(Phone1, 5, 3) + '-' + SubString(Phone1, 9, 4) From SVC00200 Where ETADTE In ('" + Date + "','" + tomorrow +"') And TechID = '" + techID + "' And SrvStat Between '20D' And '59Z'", SQL01);
    SQL01.Open();
    SqlDataReader dr = comm.ExecuteReader();

    if (!dr.HasRows) { dr.Close(); SQL01.Close(); return null;}

        while (dr.Read())
        {
            callInfo ci = new callInfo
                              {
                                  callNumber = dr[0].ToString(),
                                  customer = dr[1].ToString(),
                                  adrsCode = dr[2].ToString(),
                                  svcDesc = dr[3].ToString(),
                                  srvStat = dr[4].ToString(),
                                  poNumber = dr[5].ToString(),
                                  etaDte = dr[6].ToString(),
                                  etaTime = dr[7].ToString(),
                                  address = dr[8].ToString(),
                                  city = dr[9].ToString(),
                                  state = dr[10].ToString(),
                                  zip = dr[11].ToString(),
                                  phone = dr[12].ToString()
                              };
            list.Add(ci);
        }

    dr.Close();
    SQL01.Close();
    return list;

}

尝试使用该Web服务并从getCalls()

填充HashSet的方法
private string getCalls()
{
    callInfo defaultCall = new callInfo() {callNumber = "None"};
    var calls = inst.getCalls(_date, _techID).DefaultIfEmpty(defaultCall).ToList();
    if (calls.First().callNumber == "None")
        return "None";
    foreach (callInfo info in calls)
    {
        try
        {
            var SQL = string.Format("Insert Or Ignore Into Calls Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')", info.callNumber, info.customer, info.adrsCode, info.svcDesc, info.srvStat, info.poNumber, info.etaDte, info.etaTime, info.address, info.city, info.state, info.zip, info.phone);
            new SQLite(SQL, false);
        }
        catch (Exception ex)
        {
            return "Error " + ex.Message;
        }
    }
    return "Success";
}

var calls = inst.getCalls抛出

  

“未处理的异常:System.ArgumentNullException:参数不能   空值。参数名称:source“

当没有调用时,webmethod会返回null。但即使我声明了一个默认的空值,它仍然会创建null参数异常。如何处理空返回值?

3 个答案:

答案 0 :(得分:1)

如果inst.getCalls(_date, _techID)返回null,导致异常的以下.DefaultIfEmpty(defaultCall).ToList()不是吗?

答案 1 :(得分:0)

有时过多的在线逆火。 我将以这种方式更改您的代码:

   var calls = inst.getCalls(_date, _techID);
   if(calls == null) calls = defaultCall;

答案 2 :(得分:0)

    var result = inst.getCalls(_date, _techID)
    if (result != null)
    {
       callInfo defaultCall = new callInfo() {callNumber = "None"};
       var calls = result.ToList().DefaultIfEmpty(defaultCall)

    }

怎么回事?我认为你需要在查询列表之前检查服务调用的结果是否为非。