在将列表连接到列表框时获取 StackOverflowException
Getting StackOverflowException in connecting list to listbox
我创建了 class 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
的 class 中调用了它的方法:
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();
}
}
并在 Home
页面上调用了 getArr
:
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();
}
}
}
问题是,我在 DbBroker.cs
class 中的 Auction aukcija = new Auction();
上收到 WhosebugException
错误。我不知道为什么或如何解决它。
您正在自己内部创建 Auctions
个对象的列表 = Whosebug.
这是你的问题:
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 中实例化 AuctionService
。 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 可以充当服务层。但是,这仍然适用于教育目的。
希望对您有所帮助。
我创建了 class 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
的 class 中调用了它的方法:
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();
}
}
并在 Home
页面上调用了 getArr
:
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();
}
}
}
问题是,我在 DbBroker.cs
class 中的 Auction aukcija = new Auction();
上收到 WhosebugException
错误。我不知道为什么或如何解决它。
您正在自己内部创建 Auctions
个对象的列表 = Whosebug.
这是你的问题:
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 中实例化 AuctionService
。 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 可以充当服务层。但是,这仍然适用于教育目的。
希望对您有所帮助。