0

Below specified is c# code and i am using asp.net with c#,The below code describes the date difference it will update the table after 2 months,

cmd3.CommandText = "update TrackingFaculty_det " 
                   + "SET Type=@Type WHERE " 
                   + "DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,@Today)>60";

I need to hardcode the month value into web.config,so that if i want to change the date difference to three months or so on,so that in web.config if i make a change then the changes will occur in every c# pages where i have these lines of code.

My Web.Config is as below

  <?xml version="1.0"?>
   <!-- 
Note: As an alternative to hand editing this file you can use the 
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in 
machine.config.comments usually located in 
\Windows\Microsoft.Net\Framework\v2.x\Config 
-->
 <configuration>
<appSettings/>
<connectionStrings>
    <add name="ProjectConnectionString" connectionString="Data  S ource=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="ProjectConnectionString2" connectionString="Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="ProjectConnectionString3" connectionString="Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
    <!-- 
        Set compilation debug="true" to insert debugging 
        symbols into the compiled page. Because this 
        affects performance, set this value to true only 
        during development.
    -->

    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            <add assembly="Microsoft.Office.Interop.Word, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/></assemblies>
    </compilation>
    <!--
        The <authentication> section enables configuration 
        of the security authentication mode used by 
        ASP.NET to identify an incoming user. 
    -->

    <authentication mode="Windows"/>
    <!--
        The <customErrors> section enables configuration 
        of what to do if/when an unhandled error occurs 
        during the execution of a request. Specifically, 
        it enables developers to configure html error pages 
        to be displayed in place of a error stack trace.

    <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
        <error statusCode="403" redirect="NoAccess.htm" />
        <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
    -->
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>

2
  • so what is the question? Commented Oct 4, 2013 at 7:05
  • create a key in config file and access key in code Commented Oct 4, 2013 at 7:10

2 Answers 2

1

Add key in web.config like this

<configuration>
   ....
   <appSettings>
      <add key="Difference" value="60"/>

   </appSettings>
   ....
</configuration>

get key from web.config like this

string str_Diff = WebConfigurationManager.AppSettings["Difference"].ToString();

cmd3.CommandText = 
    "update TrackingFaculty_det " 
    + "SET Type=@Type WHERE " 
    + "DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,@Today) > " + str_Diff;
Sign up to request clarification or add additional context in comments.

6 Comments

When i added this code to my web.config then Master page cannot be accessed
if you please post your web.config file with the abbove config added ??
i have posted my web.config,make a look through it
remove <appSettings /> and paste the code that i have posted. also i noticed space in Data Source in first Connection string in your web.config. is it a typo ??
I have the date difference in number,but you have specified a string? Is there any problem if i use int in place of string?
|
1

Web.config code

   <configuration>
      ....
     <appSettings>
     <add key="Difference" value="60"/>

     </appSettings>
     ....
   </configuration>

get key from web.config

    string str_diff = Convert.ToString(ConfigurationManager.AppSettings["Difference"]);
    cmd3.CommandText = "update TrackingFaculty_det SET Type=@Type WHERE (FID=@FID) and DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,@Today)>'"+str_diff+"'";

This is what i needed and i got the solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.