string connection = "Data Source= " + @serverName + ";Initial Catalog=master;User ID= " + @rootUser + ";Password= " + @decryptedPass;
...
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[connection].ConnectionString))
Does your configuration file contain a
<connectionStrings>
setting with a
name
attribute that precisely matches the string you've built?
My guess is it doesn't, since that looks more like an actual connection string than the name of a connection string stored in the config file.
As a result,
ConfigurationManager.ConnectionStrings[connection]
will return
null
, and your attempt to retrieve its
ConnectionString
property will throw a
NullReferenceException
.
If you're not storing the connection string in the config file, then don't try to read it from the config file:
using (SqlConnection conn = new SqlConnection(connection))