0

I am using vs 2012(4.5) i use Encryption to Encrypt my ConnectionString,but when i call to

  connStr = ConfigurationManager.ConnectionStrings["SnOracle"].ConnectionString;

i get this error object is not set to an instance of an object

I getting path to MyApp.exe.Config file that dont have ConnectionStringsSection ,but Encrypted MyApp.exe.Config.config DO have ConnectionStringsSection.Meybe i need to set path to MyApp.exe.Config.config by my self or AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; need to return path to Encrypted file ?

How can i read ConnectionStringsSection from Encrypted MyApp.exe.Config.config file?

What i am doing wrong?

My code:

    string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
// This return path to-> MyApp.exe.Config   file that dont have  ConnectionStringsSection
//But Encrypted  MyApp.exe.Config.config DO have  ConnectionStringsSection
  Configuration config = ConfigurationManager.OpenExeConfiguration(path);
        ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;

                if (!section.SectionInformation.IsProtected)
                {
                    // Encrypt the section.
                    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

                }
                step = "9";
                // Remove encryption.
                // section.SectionInformation.UnprotectSection();
                // Save the current configuration.

                config.Save();
                step = "9a";
                string connStr = "";
                try
                {
                     //here i getting the error
                    connStr = ConfigurationManager.ConnectionStrings["SnOracle"].ConnectionString;

                }
                catch (Exception ex)
                {
                    //here afert it fails i insert it hard coded
                    connStr = "Data Source=xxx;User Id=xxx;Password=xxx";

                }

MyApp.exe.Config:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <appSettings>

  </appSettings>
</configuration>

MyApp.exe.Config.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
        <EncryptedData>
            <CipherData>
                <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAffcYl/Mg5kKs24jULobCqAQAAAACAAAAAAADZgAAwAAAABAAAADYu948udrIvfvh5iP1Atz/AAAAAASAAACgfgLks14VfblBt3TX0E5ZnkwAAAASK+Jz3H9lx7DWVEQGXVS2HhqURpMbgFenk6v/lO6lMOk7Gv+tEhEmLs/zNApOaYNFAAAAJ1UfmT1bhg4zzVFv9/RcjNKjRzL</CipherValue>
            </CipherData>
        </EncryptedData>
    </connectionStrings>
</configuration>
Vladimir Potapov
  • 2,147
  • 5
  • 35
  • 63
  • [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Oct 21 '14 at 07:17
  • I know that this is Null ex.I am using Encryption to Encrypt my ConnectionString and you send me link to NullReferenceException link?Are you serious?The problem that i cant read the Encrypted ConnectionString,and i get understant why i getting path to UnEncrypt file that empty . – Vladimir Potapov Oct 21 '14 at 07:22

0 Answers0