First, I have a MainWindow(obviously), and then created a new xaml page called LoginWindow.
I have a class file called Server_setup, to store database access data.
Server_setup
public class Server_setup
{
public readonly string Server = ConfigurationManager.AppSettings["Server"];
public readonly string Port = ConfigurationManager.AppSettings["Port"];
public readonly string User_ID = ConfigurationManager.AppSettings["User_ID"];
public readonly string Password = ConfigurationManager.AppSettings["Password"];
public readonly string Database = ConfigurationManager.AppSettings["Database"];
}
Inside App.config
...<appSettings>
<add key="Server" value="localhost" />
<add key="Port" value="5432" />
<add key="User_ID" value="postgres" />
<add key="Password" value="admin" />
<add key="Database" value="TestDatabase" />
</appSettings>...
Everything is working fine, user can login from LoginWindow and directed to MainWindow. The problem is when I tried to add a new page called UpdateWindow.
After much problem hunting(entering code line by line), I realized that I got OverflowException when I declaring mainWindow inside the UpdateWindow.
LoginWindow
public partial class LoginWindow : Window
{
private readonly Server_setup server_Setup = new Server_setup();
private readonly NpgsqlConnection iConnect = new NpgsqlConnection();
MainWindow mainWindow = new MainWindow();
public LoginWindow()
{
InitializeComponent();
iConnect = new NpgsqlConnection
(connectionString: $"Server = {server_Setup.Server};Port = {server_Setup.Port}; User ID = {server_Setup.User_ID};Password = {server_Setup.Password};Database = {server_Setup.Database}");
}
MainWindow
private readonly Server_setup server_Setup = new Server_setup();
private readonly NpgsqlConnection iConnect = new NpgsqlConnection();
UpdateWindow updateWindow = new UpdateWindow();
public MainWindow()
{
InitializeComponent();
iConnect = new NpgsqlConnection
(connectionString: $"Server = {server_Setup.Server};Port = {server_Setup.Port}; User ID = {server_Setup.User_ID};Password = {server_Setup.Password};Database = {server_Setup.Database}");
}
UpdateWindow
public partial class UpdateWindow : Window
{
MainWindow mainWindow = new MainWindow(); //Error happened when this code is inserted
public UpdateWindow()
{
InitializeComponent();
}
Any idea why the error occurred?
Looking at the call stack, the code is indeed recurring between MainWindow and LoginWindow
Whenever you create an instance of MainWindow
, you create an instance of UpdateWindow
. Whenever you create an instance of UpdateWindow
, you create an instance of MainWindow
... which will then create another instance of UpdateWindow
, which will create another instance of MainWindow
, etc.
We can't really tell enough about your UI to know what you're trying to achieve here, but you've got to get rid of that recursion. It seems unlikely that you really want to create a new instance of MainWindow
for each UpdateWindow
. Perhaps MainWindow
should "tell" the UpdateWindow
about itself (either through a constructor parameter or a property)?