jmease
jmease

Reputation: 2535

Handling XML Web Service Method That Returns 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;

}

Method attempting to consume that webservice and populate HashSet from getCalls()

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 throws

"Unhandled Exception: System.ArgumentNullException: Argument cannot be null. Parameter name: source"

The webmethod returns null when there are no calls just as it is supposed to. But even when I have a default if empty value declared, it still creates the null argument exception. How do I handle null return values?

Upvotes: 1

Views: 1080

Answers (3)

unnknown
unnknown

Reputation: 1775

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

    }

How bout something like that? I think you need to check the result of the service call being non-null before querying a list out of it.

Upvotes: 0

Steve
Steve

Reputation: 216243

Sometimes too much in-line backfires. I will change your code in this way:

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

Upvotes: 0

If inst.getCalls(_date, _techID) returns null isn't it the following .DefaultIfEmpty(defaultCall).ToList() that causes the Exception?

Upvotes: 1

Related Questions