We help IT Professionals succeed at work.

using .config file for Windows Application to read connection string for SQL Server gives error message

suchetawieser
on
3,041 Views
Last Modified: 2007-12-19
new to C# and I don't know what's wrong
I want to put the values for my database connection strings in a configuration file.

This is how my .config file looks
   <?xml version="1.0" encoding="utf-8" ?>
   <configuration>
     <appSettings>
       <add key="sqlConnVisitor.ConnectionString" value="Data Source=mullah\\Developer;
                   Integrated Security=SSPI; Initial Catalog=VisitorCenter" />
       <add key="sqlConnWelcome.ConnectionString" value="Data Source=mullah\\Developer;
                   Integrated Security=SSPI; Initial Catalog=Contacts" />
     </appSettings>
   </configuration>

The code where I'm connecting to the database looks like this:
public static void ConnectToDatabase()
{
   System.Configuration.AppSettingsReader configurationAppSettings = new
              System.Configuration.AppSettingsReader();

   sqlConnVisitor = new SqlConnection();
   sqlConnVisitor.ConnectionString = ((string)(configurationAppSettings.GetValue
              ("sqlConnVisitor.ConnectionString", typeof(string))));

   sqlConnWelcome = new SqlConnection();
   sqlConnWelcome.ConnectionString = ((string)(configurationAppSettings.GetValue
              ("sqlConnWelcome.ConnectionString", typeof(string))));

   sqlConnVisitor.Open();
   sqlConnWelcome.Open();
}

When I run the application I get the following error message:
   An unhandled exception of type 'System.InvalidOperationException' occurred in system.dll

   Additional information: The key 'sqlConnVisitor.ConnectionString' does not exist in the appSettings
   configuration section.

Anyone know what to do? Thanks
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
or, better yet:

private string getConnectionString(string WhichOne)
{
    return ((string)(configurationAppSettings.GetValue
              (WhichOne,typeof(string))));
}

public static void ConnectToDatabase()
{
   System.Configuration.AppSettingsReader configurationAppSettings = new
              System.Configuration.AppSettingsReader();

   sqlConnVisitor = new SqlConnection();
   sqlConnVisitor.ConnectionString = getConnectionString("sqlConnVisitor");

   sqlConnWelcome = new SqlConnection();
   sqlConnWelcome.ConnectionString = getConnectionString("sqlConnWelcome");

   sqlConnVisitor.Open();
   sqlConnWelcome.Open();
}

which is somewhat easier to read and understand.

AW
and one step further, gets you:

public static void ConnectToDatabase()
{
    sqlConnVisitor = new SqlConnection(getConnectionString("sqlConnVisitor"));
   sqlConnWelcome = new SqlConnection(getConnectionString("sqlConnWelcome"));

   sqlConnVisitor.Open();
   sqlConnWelcome.Open();
}

which is even clearner.

AW

Commented:
hehe I think my way is shorter and cleaner... :)
check to make sure the .config file has the format of executablename.exe.config in the output folder. If not then re-add a new item to the project selection the App Config file with the default filename.

Commented:
Can we close this question?
I concur.  It would appear that suchetawieser has no interest in participating in this discussion.  I suggest that the points NOT BE REFUNDED.

AW

Commented:
I agree...split the points :)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.