问题描述
代码如下:publicclassWebService1:System.Web.Services.WebService{staticprivateIOrganizationServiceGetOrganisationService(){ClientCredentialscredentials=newClientCredentials();credentials.Windows.ClientCredential=newNetworkCredential("admin","123456","");OrganizationServiceProxyproxy=newOrganizationServiceProxy(newUri("http://193.169.1.123/CRMAPP/XRMServices/2011/Organization.svc"),null,credentials,null);returnproxyasIOrganizationService;}[WebMethod]publicstringHelloWorld(){IOrganizationServiceservice=GetOrganisationService();string[]param=newstring[]{"lucky_name","lucky_baifangriqishijian","lucky_baifangrenyuanxingming","lucky_dizhi"};Entityresult=service.Retrieve("lucky_lead",newGuid("15158db1-9909-e511-80be-00155d018102"),newColumnSet(param));Stringname="";try{name=result.Attributes["lucky_baifangriqishijian"].ToString()+result.Attributes["lucky_baifangrenyuanxingming"].ToString()+result.Attributes["lucky_dizhi"].ToString()+result.Attributes["lucky_name"].ToString();}catch{name="NO";}returnname;}}
解决方案
解决方案二:
使用result.Attributes["lucky_baifangriqishijian"].ToString()前需要先判断result是否为null,在判断result.Attributes["lucky_baifangriqishijian"]是否为Null,最后再使用toString()。仅供参考。
解决方案三:
先判断下Attributes[""]是否为null,再执行ToString吧或者写个funcvarattr=result.Attributes;Func<string,string>func=(key)=>{if(attr[key]==null){returnstring.Empty;}returnattr[key].ToString();};name=func("lucky_baifangriqishijian")+func("lucky_baifangrenyuanxingming")
解决方案四:
每个字段都需要做判断么?
解决方案五:
而且根本不行,还是报错—“给定关键字不在字典中”
解决方案六:
result.Attributes是字典吧?试一下if(result.Attributes.ContainsKey(key)){result.Attributes.ContainsKey[key]....你的操作}else{给予提示或者警告}