【问题标题】:When returning a list in the web service, I get an error only if a value is returned?在 Web 服务中返回列表时,只有在返回值时才会出现错误?
【发布时间】:2020-09-15 10:09:03
【问题描述】:

列表在 api 中返回。如果数据库中有多个值,我不会收到错误消息。正确返回列表。但是当我得到一个值时,我在使用 api 时会出错。

这是我的代码;

WebService1.asmx

[WebMethod(EnableSession = true)] 
public List<AP_City> AA_Send_CountryCode(string us, string ps, string country_code) 
{ 
    AP_City menuclass = new AP_City(); 
    List<AP_City> menuliste = new List<AP_City>(); 
    DbClassSQL db = new DbClassSQL();

    DataTable dt2x = db.Doldur("SELECT * FROM AP_City where CountyCode='" + country_code + "' order by OrderNumber");


    if (dt2x.Rows.Count > 0)
    {
        for (int i = 0; i < dt2x.Rows.Count; i++)
        {
            menuclass.Id = Convert.ToInt32(dt2x.Rows[i]["Id"]);
            menuliste.Add(menuclass);
            menuclass = new AP_City();
        }
    }

    return menuliste;
}

这是我的课

namespace Deneme
{
    public class AP_City
    {
        public int Id { get; set; }
    }
}

【问题讨论】:

    标签: c# asp.net list web-services get


    【解决方案1】:

    更改以下行:

        menuclass.Id = Convert.ToInt32(dt2x.Rows[i]["Id"]);
        menuliste.Add(menuclass);
        menuclass = new AP_City();
    

    到:

        menuliste.Add(new AP_City() {Id = Convert.ToInt32(dt2x.Rows[i]["Id"])});
    

    并删除该行:

        AP_City menuclass = new AP_City();
    

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 2011-01-26
      • 2011-05-05
      相关资源
      最近更新 更多