Monday, 9 June 2014

Read and Edit web.config file in C#

Read and Update web.config file is one of the important and regular task from server side code or from different application. We can Read and Write web.config's AppSetting or SQL ConnectionString using two classes WebConfigurationManager and ConfigurationManager in C#. In this article, I am going to write C# code examples to Read and Edit Web config settings at runtime and from different application(i.e -Setup).

Read web.config file in C#

Consider the sample web.config file structure:
<configuration>
  <appSettings>
    <add key="appPath" value="C:\Prgram Files\MyApplication" />
  </appSettings>
  <connectionStrings>
    <add name="sqlInfo" connectionString="Data Source=.\SQLExpress; 
Initial Catalog=MorganDB2; Integrated Security=SSPI;" />
  </connectionStrings>
  </configuration>

Read web.config's AppSetting and ConnectionString using WebConfigurationManager:

private void ReadWebConfigSetting()
{
    string appPath = WebConfigurationManager.AppSettings["appPath"];
    string connString = WebConfigurationManager.ConnectionStrings["sqlInfo"].ToString();
}

Read web.config's AppSetting and ConnectionString using ConfigurationManager:

private void ReadWebConfigSettingbyCM()
{
    string appPath = ConfigurationManager.AppSettings["appPath"];
    string connString = ConfigurationManager.ConnectionStrings["sqlInfo"].ToString();
}

Write web.config setting using C#

Edit web.config's AppSetting and ConnectionString from Different Application:

Use the below sample code to write or edit a web.config's AppSetting and ConnectionString from different/other location.
private static void EditWebConfigFile()
{
    string webDirPath = @"C:\Prgram Files\MyApp\WebFiles";
    var vdm = new VirtualDirectoryMapping(webDirPath, true, "web.config");
    var wcfm = new WebConfigurationFileMap();
    wcfm.VirtualDirectories.Add("/", vdm);
    var configuration = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");

    var appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
    appSettingsSection.Settings["appPath"].Value = @"C:\Prgram Files\MyApp";

    var connStrSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
    connStrSection.ConnectionStrings["sqlInfo"].ConnectionString = @"Data Source=.\SQLExpress;
 Initial Catalog=MorganDB; Integrated Security=SSPI;";

    configuration.Save(ConfigurationSaveMode.Modified);
}

Edit web.config's AppSetting and ConnectionString at Run Time:

Use the below sample code to edit and update a web.config's AppSetting and ConnectionString at run time.
private void WriteWebConfigSettings()
{
    var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

    var appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
    appSettingsSection.Settings["appPath"].Value = @"C:\Prgram Files\MyApplication";

    var connStrSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
    connStrSection.ConnectionStrings["sqlInfo"].ConnectionString = @"Data Source=.\SQLExpress; 
Initial Catalog=MorganDB2; Integrated Security=SSPI;"; ;

    configuration.Save(ConfigurationSaveMode.Modified);
}

Wednesday, 4 June 2014

Timer in JavaScript

We have following three built-in JavaScript functions to execute a JavaScript function or block of code with regular interval by specified delay.

1. setTimeOut()

The setTimeout() function is commonly used to call a function for one time after the specified delay.
<html>
<head>
<script>
var timerId
function updateTime() {
 var date = new Date()
  var hours = date.getHours()
  if (hours < 10) hours = '0'+hours
  document.getElementById('hour').innerHTML = hours
  var minutes = date.getMinutes()
  if (minutes < 10) minutes = '0'+minutes
  document.getElementById('min').innerHTML = minutes
  var seconds = date.getSeconds()
  if (seconds < 10) seconds = '0'+seconds
  document.getElementById('sec').innerHTML = seconds
  timerId = setTimeout(updateTime, 1000)
}

function startClock() {  
  if (timerId) return
  updateTime()
}

function stopClock() {
  clearTimeout(timerId)
  timerId = null
}
</script>
</head>
<body>
<label id="hour">hh</label>:<label id="min">mm</label>:<label id="sec">ss</label>
<input type="button" onclick="startClock()" value="Start">
<input type="button" onclick="stopClock()" value="Stop">
</body>
</html>

2. setInterval()

The setInterval() function is commonly used to execute a function repeatedly in the specified interval.
<html>
<head>
<script>
var timerId
function updateTime() {
 var date = new Date()
  var hours = date.getHours()
  if (hours < 10) hours = '0'+hours
  document.getElementById('hour').innerHTML = hours
  var minutes = date.getMinutes()
  if (minutes < 10) minutes = '0'+minutes
  document.getElementById('min').innerHTML = minutes
  var seconds = date.getSeconds()
  if (seconds < 10) seconds = '0'+seconds
  document.getElementById('sec').innerHTML = seconds  
}

function startClock() {  
  if (timerId) return

  timerId = setInterval(updateTime, 1000)
  updateTime()  // start immediately, don't wait 1 sec until setInterval triggers  
}

function stopClock() {
  clearInterval(timerId)
  timerId = null
}
</script>
</head>
<body>
<label id="hour">hh</label>:<label id="min">mm</label>:<label id="sec">ss</label>
<input type="button" onclick="startClock()" value="Start">
<input type="button" onclick="stopClock()" value="Stop">
</body>
</html>

3. requestAnimationFrame()

The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser call a specified function to update an animation before the next repaint. The method takes as an argument a callback to be invoked before the repaint.

Refer this nice example: http://jsfiddle.net/XQpzU/4358/light/