Link to home
Start Free TrialLog in
Avatar of suchetawieser
suchetawieser

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of glsac
glsac
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
hehe I think my way is shorter and cleaner... :)
Avatar of Joeisanerd
Joeisanerd

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.
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
I agree...split the points :)