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

乔万·加吉奇(Jovan Gajic)

我创建了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();
     }
 }

getArrHome页面上调用

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

尼克·戴比尔

您正在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
    }
}

更新

您将需要AuctionService在DbBroker中实例化DbBroker不再继承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可以充当Service层。但是,这仍然可以用于教育目的。

我希望这有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章