0

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?

Philo
  • 1,931
  • 12
  • 39
  • 77

1 Answers1

3

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.

John Gibb
  • 10,603
  • 2
  • 37
  • 48
  • Thanks! I updated my code, basically I don't see any value being added, which would explain the error. – Philo Dec 10 '13 at 18:44
  • 1
    You'll need to add that value then... we can't really help tell you what that value should be though, but the fact that it's missing is why you're getting that error. – John Gibb Dec 10 '13 at 18:47
  • yes, agreed. thanks for pointing me to the right direction. appreciate it. :) – Philo Dec 10 '13 at 18:55
  • although a bit confused why ClientSettingsProvider.ServiceUri is being used. thats kind of weird. – Philo Dec 10 '13 at 18:57
  • interesting, some googling led to: http://stackoverflow.com/questions/11963270/purpose-of-clientsettingsprovider-serviceuri-in-app-config – John Gibb Dec 10 '13 at 19:02