Is my machine playing me today?
When I run the TestConnectionString() method of this object I get the error at the connection string setting.
public class CustomerDAL { string connectionString = ConfigurationManager.ConnectionStrings['myConnection'].Name; public CustomerDAL() { } public string TestConnection() { System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString); conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { return 'Open'; } else { return 'Close'; } } }
If
ConnectionStrings['myConnection']returns null, then dereferencing it to get theNameproperty will fail in the constructor. Is that definitely not where the bug is?Why not put a breakpoint on that line and take a look in
ConfigurationManager.ConnectionStringsto check what it thinks it’s got – and check very carefully for typos.After that, put a breakpoint on the first line of the method, and check what the value of
connectionStringis. Passing innullto theSqlConnectionconstructor doesn’t actually throw an exception, but you’d get anInvalidOperationExceptionwhen you try to open it.