Philo
Philo

Reputation: 1989

ConnectionManager.AppSettings

I stumbled across an SQL insertion in C# and I am trying to figure the ConnectionManager.Appsetting. Can anyone help me understand the significance of the following code:-

 strSQL = "INSERT INTO [History]"
                + " (xxxx, yyyy, zzzz)"
                + " values (@xxxx, @yyyy, @RoleKey)";
            Connection.Open();
            command = new SqlCommand(strSQL, Connection);
            command.Parameters.AddWithValue("@xxxx", xx);
            command.Parameters.AddWithValue("@yyyy", "1");
            command.Parameters.AddWithValue("@RoleKey", ConfigurationManager.AppSettings["RoleKey"].ToString());
            command.ExecuteNonQuery();
            Connection.Close();

Now when I look at the table, instinctively I want the value of RoleKey to be table driven from another table that maintains the values of RoleKey with individual description fields for each value.

I am trying to understand what the statement:-

command.Parameters.AddWithValue("@RoleKey", ConfigurationManager.AppSettings["RoleKey"].ToString());

is trying to do? I tried to run this insertion via C# and there is a null exception error on this line.

I have been referring to MSDN thus far:-http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings%28v=vs.110%29.aspx

In app.config I see:-

<configuration>
  <appSettings>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>

Can anyone give me some further info?

Upvotes: 0

Views: 621

Answers (1)

John Gibb
John Gibb

Reputation: 10773

ConfigurationManager.AppSettings comes from either the web config in an ASP.Net application, or the App.config in a regular application.

Look for something like this:

<configuration>
  <appSettings>
    <add key="RoleKey" value="MyRoleKeyValue" />
  </appSettings>
</configuration>

That way you can change rolekey without having to recompile the code.

Upvotes: 3

Related Questions