在将列表连接到列表框时获取StackOverflowException

时间:2015-12-25 16:05:11

标签: c# asp.net stack-overflow

我已经使用此代码创建了类Auction.cs

namespace WebApplication5
{
    public class Auction
    {
        public string Productname { get; set; }
        public string Lastbidder { get; set; }
        public int Bidvalue { get; set; }

        private List<Auction> listaAukcija = new List<Auction>();

        public List<Auction> ListaAukcija
        {
            get { return listaAukcija; }
            set { listaAukcija = value; }
        }

        public void getAll()
        {
            using (SqlConnection conn = new SqlConnection(@"data source=JOVAN-PC;database=aukcija_jovan_gajic;integrated security=true;"))
            {
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"SELECT a.id AS aid, p.name AS pn, u.name AS un, a.lastbid AS alb FROM (Auction a INNER JOIN Product p ON a.productid = p.id) INNER JOIN User u ON a.lastbider = u.id";
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    listaAukcija.Clear();

                    while (reader.Read())
                    {
                        Auction auction = new Auction();

                        if (reader["aid"] as int? != null)
                        {
                            auction.Productname = reader["pn"] as string;
                            auction.Lastbidder = reader["un"] as string;
                            auction.Bidvalue = (int)reader["alb"];
                        }

                        listaAukcija.Add(auction);
                    }
                }
            }
        }

        public override string ToString()
        {
            return base.ToString();
        }
    }
}

我在另一个名为DbBroker.cs的类中调用了它的方法:

 public class DbBroker : Home
 {
     Auction aukcija = new Auction();

     public void executeQuery()
     {
         aukcija.getAll();
     }

     public void getArr()
     {
         List<string[]> lista = aukcija.ListaAukcija.Cast<string[]>().ToList();
         var x = ListBox1.Text;
         x = lista.ToString();
     }
 }

getArr页面上调用Home

public partial class Home : System.Web.UI.Page
{
    DbBroker dbb = new DbBroker();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label3.Text = Session["Username"].ToString();
            dbb.getArr();
        } 
    }
}

问题是,StackOverflowException课程中的Auction aukcija = new Auction();出现DbBroker.cs错误。我不知道为什么或如何解决它。

1 个答案:

答案 0 :(得分:3)

您正在创建一个Auctions个对象的列表= Stackoverflow

这是你的问题:

public class Auction {
    private List<Auction> listaAukcija = new List<Auction>();
}

您需要将Auction模型与获取数据的服务或存储库分开。

例如:

//the model
public class Auction {
    public string Productname { get; set; }
    public string Lastbidder { get; set; }
    public int Bidvalue { get; set; }

    public override string ToString()
    {
        return base.ToString();
    }
}

//the service (or can replace this with a repository)
public class AuctionService {
    private List<Auction> listaAukcija = new List<Auction>();

    public List<Auction> ListaAukcija
    {
        get { return listaAukcija; }
        set { listaAukcija = value; }
    }

    public void getAll()
    {
        //get the data and populate the list
    }
}

<强>更新

您需要在DbBroker中实例化AuctionServiceDbBroker不再继承Home(已注释掉)。

public class DbBroker //: Home <-- circular reference
{
    AuctionService auctionService = new AuctionService();

    public void executeQuery()
    {
        auctionService.getAll();
    }

    public void getArr()
    {
        string[] lista = auctionService.ListaAukcija.ConvertAll(obj => obj.ToString()).ToArray();

        ListBox1.Text = string.Join("\n", lista);
    }
}

和Page_Load() - 你没有调用executeQuery()函数来填充列表。

public partial class Home : System.Web.UI.Page
{
    DbBroker dbb = new DbBroker();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label3.Text = Session["Username"].ToString();
            dbb.executeQuery(); //populate list.
            dbb.getArr(); //convert to string and update textbox
        } 
    }
}

PS。使用新的更新,AuctionService实际上应该是存储库,DbBroker可以充当服务层。但是,这仍然适用于教育目的。

我希望这会有所帮助。