I have a Form1 acting as login form which has 4 different user controls(custom controls) for different roles to get logged in. each user control has the the below code. have a look at.
public static string ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(ConnectionString);
in this code i am accessing the connection string from app.config. the code is working fine, i am able to insert, update, view data in database but the GUI disappears when i open the Form1 design and shows many errors like object reference not set to instance. Every error is taking me to the line of code where i am accessing the connection string. when i comment the line of code errors goes away and the GUI appears.
I have two questions: 1. I want to know what is the right way to avoid the above error. How should i write the code to access the connection string which doesn't throw any exception and affect GUI. 2. I want to close the Form1(Login Form) from the user control like when the user enter the right credential and logged in to their dashboard the Form1 which contains the user control should be close not hide or run in background. I have used this code but didn't work for me.
((Form)this.TopLevelControl).Close();