-1

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();
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Cleptus Apr 07 '20 at 10:29
  • You should not close form from control. Control should have event. Form should subscribe to it and do whatever it needs to do (close, etc.) – Access Denied Apr 07 '20 at 10:31

1 Answers1

0

Declaring it as public static string ConnectionString will make it accessible anywhere, but you should not set it ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; on the same line.

Because every time someone tries to access this value would expect ConfigurationManager to be available and hence you are getting the Null reference exception.

To solve this initialize the variable ConnectionString under a static constructor - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors

Carbine
  • 7,849
  • 4
  • 30
  • 54
  • Look closely at the initialisation... looks like it's a field, not a property. `=` means it's being assigned once (although could be reassigned as it's not readonly). If it were a property using `=>` THEN it would be accessing ConfigurationManager each and every time the property is accessed. As it stands, it's more likely that the error is caused by the assignment occurring too soon (`ConfigurationManager` or `ConnectionStrings` hasn't yet been instantiated). A static constructor wouldn't change this. – IAmJersh Apr 07 '20 at 10:49
  • Thanks. It worked. Can you please also tell me how i can close the Windows Form from user control c#? – Aaric Aaiden Apr 07 '20 at 11:39