Jovan Gajic
Jovan Gajic

Reputation: 101

Getting StackOverflowException in connecting list to listbox

I have created class Auction.cs with this code behind :

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

I called it's methods in another class called 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();
     }
 }

And called getArr on Home page :

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

The problem is, I get StackOverflowException error on Auction aukcija = new Auction(); in DbBroker.cs class. I don't know why or how to solve it.

Upvotes: 4

Views: 128

Answers (1)

Nick De Beer
Nick De Beer

Reputation: 5262

You are creating a list of Auctions objects within itself = Stackoverflow.

This is your problem:

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

You will need to separate the Auction model from the service or repository that gets the data.

For example:

//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
    }
}

UPDATE

You will need to instantiate the AuctionService in DbBroker. DbBroker does not inherit Home anymore (commented out).

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

and on Page_Load() - you did not call executeQuery() function to populate the list.

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. With the new update, AuctionService should actually be the repository, and DbBroker can act as the Service layer. However, this still works for educational purposes.

I hope this helps.

Upvotes: 3

Related Questions