Getting StackOverflowException in connecting list to listbox - c#

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.

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.

Related

Unable to call method's objects into the Form

As you can see I instantiated the classes I need into the form_load, in order to use methods and classes features. The problem is that I need to Call the item NuovoCliente from CreateClientemethod, but I don't know how to do, since intellisense, even when I try to type, does not show any link to NuovoCliente.
The class you can see with the method is ClienteModel.
Which is basically structured like this:
public class ClienteModel
{
public int IDCliente { get; set; }
public string Cognome { get; set; }
public string Nome { get; set; }
public string Indirizzo { get; set; }
}
This is my method, which is placed in DBMemoryManager class:
public class DBMemoryManager : DBManager
{
//Array
ClienteModel[] MemoryClienti = new ClienteModel[0];
public int CreateCliente(ClienteModel model)
{
ClienteModel NuovoCliente = new ClienteModel();
int MaxCID = MemoryClienti.Select(ClienteModel => ClienteModel.IDCliente).Max();
MemoryClienti[0] = NuovoCliente;
NuovoCliente.IDCliente = MaxCID++;
return NuovoCliente.IDCliente;
}
This is how my Form start:
public partial class Form1 : Form
{
DBMemoryManager dbMemoryManager = null;
ClienteModel clienteModel = null;
OrdineModel ordineModel = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dbMemoryManager = new DBMemoryManager();
clienteModel = new ClienteModel();
ordineModel = new OrdineModel();
}
Return a ClienteModel from this method.
public ClienteModel CreateCliente(ClienteModel model)
{
ClienteModel NuovoCliente = new ClienteModel();
int MaxCID = MemoryClienti.Select(ClienteModel => ClienteModel.IDCliente).Max();
MemoryClienti[0] = NuovoCliente;
NuovoCliente.IDCliente = MaxCID++;
return NuovoCliente;
}
Now access data from Form_load
public partial class Form1 : Form
{
ClienteModel clienteModel = null;
OrdineModel ordineModel = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
clienteModel = new ClienteModel();
ordineModel = new OrdineModel();
DBMemoryManager dbMemoryManager = new DBMemoryManager(); //initialize here
ClienteModel nuovoCliente = dbMemoryManager.CreateCliente(clienteModel)
//here you can get all data from nuovoCliente
}
here is the data.

Send Parameters using Frame.Navigate in UWP

A class to temporarily store my input value here.
namespace App2
{
class methC
{
public string tb1 { get; set; }
public string tb2 { get; set; }
public string tb3 { get; set; }
public string tb4 { get; set; }
public string tb5 { get; set; }
public string tb6 { get; set; }
public methC(string tb1, string tb2, string tb3)
{
this.tb1 = tb1;
this.tb2 = tb2;
this.tb3 = tb3;
//this.right1 = right1;
//this.right2 = right2;
//this.right3 = right3;
//this.Box = Box;
//this.Grade = Grade;
}
}
}
first page that take in the parameter
namespace App2
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
Frame.Navigate(typeof(BlankPage1));
}
}
}
How to get the data stored in tb1,tb2,tb3 to display in BlankPage1?
namespace App2
{
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
string four, five, six;
four = tb4.Text;
five = tb5.Text;
six = tb6.Text;
box.Text = four + five + six;
}
private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
{
}
}
}
How's the thing suppose to work, MainPage take in 3 entry, store in methC and get from BlankPage to display tb1,tb2,tb3. How can i make this thing work?
As long as you Navigate to another page using a Type parameter you can't use a constructor to pass the data. so Microsoft provided another way of doing that work.
You can pass your object using this overload of Navigate function:
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
string Param = JsonConvert.SerializeObject(mc);
Frame.Navigate(typeof(BlankPage1), Param);
override OnNavigatedTo in the BlankPage1:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
methC myData = JsonConvert.DeserializeObject<methC>((string)e.Parameter);
Labelx.Text = myData.x;
, ....
base.OnNavigatedTo(e);
}
this is from MSDN
and the worst case is to assign them through static members, so in the BlankPage1 class you can add:
public static methC Data { get; set; }
public BlankPage1()
{
mylabel.Text = Data.X;
, ....
}
and you can assign the values before Navigation:
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
BlankPage1.Data = mc;
Frame.Navigate(typeof(BlankPage1));
In your mainpage, you can pass the parameter of the class object directly.
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
Frame.Navigate(typeof(BlankPage1), mc);
In your next page, you can receive it as a class object
protected override void OnNavigatedTo(NavigationEventArgs e) {
navigationHelper.OnNavigatedTo(e);
methC item = e.Parameter as methC;
}

How to share a list of objects without giving ability to modify their state?

Let's say I have a class StockMarket which has a list of Companies.
class StockMarket : IStock
{
private static List<IObserverPush> observersPush;
private static List<IObserverPull> observersPull;
public static List<Company> Companies { get; private set; }
public StockMarket()
{
observersPush = new List<IObserverPush>();
observersPull = new List<IObserverPull>();
Companies = new List<Company>() { new Company("Unilever", "UNA", 47.72, 0.77, 1.63, -3.45, "135B"),
new Company("ING Groep", "INGA", 13.40, -0.07, -0.50, -12.38, "60.4B"),
new Company("ArcelorMittal", "MT", 29.50, 0.14, 0.48, 36.05, "54.6B"),
new Company("ASML Holding", "ASML", 167.40, 2.00, 1.21, 36.49, "53.3B"),
new Company("Heineken", "HEIA", 87.66, -0.02, -0.02, 2.80, "49B"),
new Company("RELX", "REN", 18.15, 0.17, 0.95, -0.22, "38.9B"),
new Company("Philips", "PHIA", 35.49, 0.17, 0.47, 7.61, "33.3B"),
new Company("Unibail Rodamco", "UL", 196.40, -0.15, -0.08, -16.78, "20.3B"),
new Company("Akzo Nobel", "AKZA", 75.68, -0.16, -0.21, 0.33, "19.4B"),
new Company("Altice", "ATC", 7.58, 0.16, 2.16, -66.30, "17.6B")};
Thread thread = new Thread(SimulateMarket);
thread.Start();
}
public void Subscribe(IObserverPull o)
{
observersPull.Add(o);
o.UpdateMarket();
}
public void Unsubscribe(IObserverPull o)
{
observersPull.Remove(o);
}
public void Subscribe(IObserverPush o)
{
observersPush.Add(o);
o.UpdateMarket(Companies);
}
public void Unsubscribe(IObserverPush o)
{
observersPush.Remove(o);
}
public void NotifyObservers()
{
foreach(IObserverPush o in observersPush)
{
o.UpdateMarket(Companies);
}
foreach(IObserverPull o in observersPull)
{
o.UpdateMarket();
}
}
public void SimulateMarket()
{
while(observersPush.Count + observersPull.Count > 0)
{
//randomly change property values of companies
//and notify the observers about the changes
}
}
}
Company class has some properties.
public class Company
{
public string Name { get; private set; }
public string Symbol { get; private set; }
public double Price { get; set; }
public double Change { get; set; }
public double ChangePercentageDay { get; set; }
public double ChangePercentageYear { get; set; }
public string Capital { get; private set; }
public Company(string name, string symbol, double price, double change, double changePercentageDay,
double changePercentageYear, string capital)
{
Name = name;
Symbol = symbol;
Price = price;
Change = change;
ChangePercentageDay = changePercentageDay;
ChangePercentageYear = changePercentageYear;
Capital = capital;
}
}
The Forms have references to the StockMarket and they use it to retrieve data about the companies and to display it.
Form 1
public partial class ConcreteObserverPush : Form, IObserverPush
{
private StockMarket stockMarket;
public ConcreteObserverPush()
{
InitializeComponent();
stockMarket = new StockMarket();
stockMarket.Subscribe(this);
}
public void UpdateMarket(List<Company> companies)
{
stockMarketListView.Items.Clear();
foreach(Company c in companies)
{
ListViewItem item = new ListViewItem(c.Symbol);
item.SubItems.Add(c.Price.ToString());
item.SubItems.Add(c.Change.ToString());
item.SubItems.Add(c.ChangePercentageDay.ToString() + "%");
stockMarketListView.Items.Add(item);
}
}
private void ConcreteObserverPush_FormClosing(object sender, FormClosingEventArgs e)
{
stockMarket.Unsubscribe(this);
}
}
Form 2
public partial class ConcreteObserverPull : Form, IObserverPull
{
private StockMarket stockMarket;
public ConcreteObserverPull()
{
InitializeComponent();
stockMarket = new StockMarket();
stockMarket.Subscribe(this);
}
public void UpdateMarket()
{
stockMarketListView.Items.Clear();
foreach (Company c in StockMarket.Companies)
{
ListViewItem item = new ListViewItem(c.Symbol);
item.SubItems.Add(c.Name);
item.SubItems.Add(c.Price.ToString());
item.SubItems.Add(c.Change.ToString());
item.SubItems.Add(c.ChangePercentageDay.ToString() + "%");
item.SubItems.Add(c.ChangePercentageYear.ToString() + "%");
item.SubItems.Add(c.Capital);
stockMarketListView.Items.Add(item);
}
}
private void ConcreteObserverPull_FormClosing(object sender, FormClosingEventArgs e)
{
stockMarket.Unsubscribe(this);
}
}
The problem is that if the Form gets the list of companies through the property on StockMarket it can change their state. However, I want only StockMarket to have the ability to change the state of the company.
So what would be the best way to share Company state with Form when requested and preventing the Form from modifying it.
I know that a possible solution would be to return clones of Company objects, but I believe there should be a better solution.
Any help is appreciated!
The general gist of this would be to make your Company object immutable. Then you would add methods to the StockMarket object to manipulate the Company list and replace entries with new ones when you want to change a value.
Here's a quick example put together in LINQPad of making the Company class immutable and adding an UpdatePrice method to the StockMarket class.
Whether you want to be able to manipulate the Companies property from outside the StockMarket can be handled by returning the list as ReadOnlyCollection so that it's size can't be manipulated by a consumer.
void Main()
{
var sm = new StockMarket();
sm.Companies.Add(new Company("Test", "TST", 50, 0));
sm.UpdatePrice("Test", 45);
var testCompany = sm.Companies.First(x => x.Name == "Test");
Console.WriteLine($"{testCompany.Name},{testCompany.Symbol},{testCompany.Price},{testCompany.Change}");
//Output: Test,TST,45,-5
}
class StockMarket
{
public List<Company> Companies { get; private set; } = new List<Company>();
public void UpdatePrice(string name, double price) {
var index = Companies.FindIndex(x => x.Name == name);
if(index >= 0)
{
var previous = Companies[index];
Companies[index] = new Company(previous.Name, previous.Symbol, price, price - previous.Price);
}
}
}
class Company
{
public Company(string name, string symbol, double price, double change) {
Name = name;
Symbol = symbol;
Price = price;
Change = change;
}
public string Name { get; }
public string Symbol { get; }
public double Price { get; }
public double Change { get; }
///...
}
This would be a solution:
Create the Company class as a Private Inner Class inside of the StockMarket class, that way it'd only be accessible inside of it, and then provide an interface that only includes the get of all the properties and make Company implement it. You would have to make StockMarket's Company list to be the Interface's type.
Any modification you'd have to do you'd do it by casting the interface's List objects into the original class type.
Example:
class Program
{
public static StockMarket stockMarket = new StockMarket();
static void Main(string[] args)
{
}
}
public interface ICompany
{
string Name { get; }
}
public class StockMarket
{
public StockMarket()
{
Companies = SomeWildFunctionThatRetrievesAllCompanies();
}
public void OneWildFunctionThatModifiesACompany()
{
Company dunno = (Company)Companies[0];
dunno.Name = "Modification Made Possible";
}
private List<ICompany> SomeWildFunctionThatRetrievesAllCompanies()
{
return new List<ICompany>(new List<Company>());
}
public List<ICompany> Companies { get; private set; }
private class Company : ICompany
{
public string Name { get; set; }
}
}
Try this:
class Company
{
public Company(Type type,string name,string symbol,double price, double change)
{
if (type.Name == "StockMarket")
{
Name = name;
Symbol = symbol;
Price = price;
Change = change;
}
}
private string Name { get; set; }
private string Symbol { get; set; }
private double Price { get; set; }
private double Change { get; set; }
///...
}
This will allow you to change the state only if the type is StockMarket
like:
class StockMarket
{
public List<Company> Companies { get; set; }
public StockMarket()
{
Companies = new List<Company>();
}
public StockMarket someMethod()
{
//You can change the state here
StockMarket s = new StockMarket();
s.Companies.Add(new Company(this.GetType(), "aa", "_", 123, 1234));
return s;
}
//...
}
Now you cannot change the state here:
public partial class Observer: Form
{
private StockMarket stockMarket;
public ConcreteObserverPull()
{
InitializeComponent();
stockMarket = new StockMarket();
//Here you cannot change the state
stockMarket.Companies.Add(new Company(this.GetType(), "aa", "_", 123,12));
}
//...
}
Sorry, I don't know C#, but as an idea, you can wrap returned entities with decorator or proxy, which will throw an exception in case of trying to modify state of a company.
Returning clones with fields set as readonly is the safest way to go.

Binding combobox BindingList Winforms C#

Ok, so I have been facing this issue several times now but I always seemed to figure it out.
This time, ive been stuck and ive tried multiple things, but nothing is working.
I have a combobox of customers in a customer form. When i create a new customer, my combobox is not updating. It only refreshes when closing and re-opening the form.
I am aware of the "ugly and quick" solutions, but I don't want to use this unless I have no other choise. So ive been using BindingLists because of the events... BUT this is not working in my case.
Ive been staring to this for a time now, let it aside, tried again, but keep on failing.
Does anyone know whats missing in this puzzle?
Now the code: Presenter
public class CustomerPresenter
{
private tbl_customer customer = new tbl_customer();
private CustomerView customerView = new CustomerView();
public CustomerPresenter(tbl_customer customer, CustomerView customerView)
{
this.customer = customer;
this.customerView = customerView;
customerView.customerPresenter = this;
}
// Get a list of customers
public List<tbl_customer> customerList;
public BindingList<tbl_customer> getCustomers()
{
using (var customers = new DBCrownfishEntities())
{
var customer = from c in customers.tbl_customer
where c.IsDeleted == false
select c;
this.customerList = customer.ToList();
var listBinding = new BindingList<tbl_customer>(this.customerList);
return listBinding;
}
}
public void testAdd()
{
tbl_customer customer = new tbl_customer();
customer.CustomerPrefix = "Pref";
customer.CustomerName = "Name";
customer.CustomerAddress = "Address";
customer.CustomerPostalCode = "1111";
customer.CustomerCity = "City";
customer.CustomerCountry = "Country";
customer.CustomerCountryCode = "BE";
customer.CustomerVat = "123456789";
customer.hasVatNumber = true;
try
{
using (var cust = new DBCrownfishEntities())
{
cust.tbl_customer.Add(customer);
cust.SaveChanges();
MessageBox.Show("A new customer is succesfully added!");
}
}
catch (EntityException exception)
{
MessageBox.Show(exception.InnerException.Message.ToString(), "Error Connecting database");
}
catch (Exception)
{
throw;
}
}
}
View:
BindingSource bsCustomers = new BindingSource();
private void CustomerView_Load(object sender, EventArgs e)
{
bsCustomers.DataSource = customerPresenter.getCustomers();
cbCustomers.DataSource = bsCustomers;
cbCustomers.DisplayMember = "CustomerName";
cbCustomers.ValueMember = "CustomerId";
radioButton1_CheckedChanged(null, null);
}
private void button1_Click_1(object sender, EventArgs e)
{
customerPresenter.testAdd();
}
Model:
public partial class tbl_customer
{
public tbl_customer()
{
this.tbl_invoices = new HashSet<tbl_invoices>();
}
public int CustomerID { get; set; }
public string CustomerPrefix { get; set; }
public string CustomerName { get; set; }
public string CustomerEmailaddress { get; set; }
public string CustomerAddress { get; set; }
public string CustomerPostalCode { get; set; }
public string CustomerCity { get; set; }
public string CustomerVat { get; set; }
public string CustomerCountryCode { get; set; }
public string CustomerCountry { get; set; }
public bool IsDeleted { get; set; }
public bool hasVatNumber { get; set; }
public virtual ICollection<tbl_invoices> tbl_invoices { get; set; }
}

How can I receipt users?

We are two classes :
First class is:
public class Team
{
public Team()
{
UsersMyTeam = new List<User>();
ID = "";
}
public string ID { set; get; }
public string NameTeam { set; get; }
public List<User> UsersMyTeam { set; get; }
public override string ToString()
{
return "Team";
}
}
Second class is :
public class User
{
public string ID { get; set; }
public string Name { get; set; }
public string LName { get; set; }
public string IsActive { get; set; }
public string Date { get; set; }
public string TeamID { get; set; }
public override string ToString()
{
return "User";
}
}
I use of class by code:
protected void btnTest_Click(object sender, EventArgs e)
{
DALTableIO DTIO = new DALTableIO();
List<Team> listUser = new List<Team>();
Team myTeam = new Team();
myTeam.ID = "426f63a7-7f42-485f-8407-67c680f9e358";
foreach (object item in DTIO.GetAll(myTeam))
{
listUser.Add((Team)item);
}
}
I have a class named DALTableIO that get values from database and Put them into object of class:
public class DALTableIO:DALBase
{
public List<object> GetAll(object MyClass)
{
SqlDataReader re = ExecuteReader(CommandType.StoredProcedure, string.Concat("GetAll", MyClass.ToString()), new SqlParameter[]{
});
List<object> list = new List<object>();
try
{
while (re.Read())
{
Type t=MyClass.GetType();
// creat new Class
object item = Activator.CreateInstance(t);
// start Fill Class
foreach (PropertyInfo property in MyClass.GetType().GetProperties())
{
//when we have property of list<object>
if (property.PropertyType.Name.ToLower() == "list`1")
{
//how can i create list<users>
//how can i create user
//how can i do list<users> =getAll(user);
//how can i do property=list<users>;
continue;
}
if (property.PropertyType.Name.Substring(0, 3) == "Int")
item.GetType().GetProperty(property.Name).SetValue(item, int.Parse(re[property.Name].ToString()));
else
switch (property.PropertyType.Name)
{
case "String":
item.GetType().GetProperty(property.Name).SetValue(item, re[property.Name].ToString());
break;
case "Decimal":
item.GetType().GetProperty(property.Name).SetValue(item, decimal.Parse(re[property.Name].ToString()));
break;
}
}
list.Add(item);
}
if (!re.IsClosed)
re.Close();
re.Dispose();
SqlConnection.ClearAllPools();
return list;
}
catch (Exception err)
{
throw new Exception( err.Message);
}
}
Tell me how can I fill users list<>. I want send one class user to getAll() for give all users. thanks.
Try the following code, it may help you.
if (property.PropertyType.Name.ToLower() == "list`1")
{
if (property.PropertyType.Name.ToLower() == "list`1")
{
var type = property.PropertyType;
var it = type.GetGenericArguments()[0];
var users = Activator.CreateInstance(type); // list of user
var user = Activator.CreateInstance(it); //user
user.GetType().GetProperty("ID").SetValue(user, 1, null);
user.GetType().GetProperty("Name").SetValue(user, "Name1", null);
var add = type.GetMethod("Add");
add.Invoke(users, new[] { user });
}
}

Categories

Resources