Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if (conection.State != ConnectionState.Closed) { conection.Close(); }

What I have tried:

What makes the above code free from Null Dereference? Kindly suggest
Posted
Updated 28-Sep-17 23:30pm
Comments
Madhava_v 29-Sep-17 5:33am    
Thanks
CPallini 29-Sep-17 6:42am    
Only checking for null before would prevent the exception.

1 solution

Nothing. If connection is null, it will still throw an exception.
Try this:
if (connection != null && conection.State != ConnectionState.Closed) 
   {
   conection.Close(); 
   }
But better, use a using block around your connection creation so it is automatically closed and disposed when it goes out of scope.
 
Share this answer
 
Comments
CPallini 29-Sep-17 6:41am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900