在Web服务中返回列表时,仅在返回值时才会出现错误?

时间:2020-05-28 07:03:05

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

列表以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; }
    }
}

1 个答案:

答案 0 :(得分:0)

更改以下几行:

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