2010-09-27

Generic cache manager

The following class is a generic cache manager. It requires a reference to System.Web.

public static class CacheManager
{
    public static T GetObject<T>(string key)
    {
        if (System.Web.HttpRuntime.Cache.Get(key) != null) return (T)System.Web.HttpRuntime.Cache.Get(key);
        return default(T);
    }

    public static void AddObject<T>(T customObject, string key, TimeSpan duration)
    {
        if (System.Web.HttpRuntime.Cache.Get(key) == null)
            System.Web.HttpRuntime.Cache.Insert(key, customObject, null, System.Web.Caching.Cache.NoAbsoluteExpiration, duration);
    }
}

2010-09-23

SQL Server 2008: No connection could be made because the target machine actively refused it

An old problem which still causes me some trouble now and again.

So I got the following error:

No connection could be made because the target machine actively refused it. Microsoft SQL Server Error 10061

So first thing to check is wether or not the server is set up to accept remote connections.

  1. Open up SQL Server Management Studio (SSMS).
  2. Right-click on the server instance, select Properties…
  3. Click on the “Connections” option on the left side.
  4. Verify that the “Allow remote connections to this server” option is checked.

If it was not enabled, you might try to restart the sql server service by running “NET STOP mssqlserver” and “NET START mssqlserver” in a command window. You need to restart the service for any configuration changes to take effect.

Second thing you might check is if the server has enabled TCP/IP.

  1. Open SQL Server Configuration Manager.
  2. Expand the SQL Server Network Configuration node.
  3. Make sure that TCP/IP is enabled.

Remember again that you must restart Sql Server for changes to be effective.

Final thing is to go into the Surface Area configuration and enable the functionality you need. This used to be a separate configuration tool in SQL Server 2005, but now it is integrated into SSMS.

  1. Open up SQL Server Management Studio (SSMS).
  2. Right-click on the server instance, select Facets…
  3. Select the “Surface Area Configuration” facet from the dropdown at the top.
  4. You may at least want to set the “AdHocRemoteQueriesEnabled” to true, and maybe some of the other options as well.

Remember to restart SQL Server.

2010-09-13

Pregnancy Ticker Screen Saver

I have created a HTML page to be used with the HTML Screen Saver from http://myweb.tiscali.co.uk/djmclean/htmlscreensaver.html.

The actual screen saver must first be installed from the link above.

After it has been installed, create a HTML Page and paste the following markup into it, and then save it somewhere (preferably in a dedicated new directory).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body bgcolor="#0080C0" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" scroll="no">
<div id="tickerDiv" style="width:1600px;height:1000px; background-image:url('scrback.jpg'); background-repeat: no-repeat; background-position:center center; border-width: 0; margin: 0 0 0 0;" >
<table id="kwsTickerLayoutTable" border="0" cellspacing="2" cellpadding="1" style="background-color:#FFEEFF;text-align:left;position:absolute;">
<tr><td rowspan="3" style="background-color:green;"><a id="kwsTickerCountdownUrl" href="" style="border:0;"><img id="kwsTickerCountdownImage" width="100px" alt="" src="" style="border: 1px solid #EEDDEE;" /></a></td><td style="font-style:normal; font-family: Arial; font-size: x-large; font-weight: bold; font-variant: normal; color: #008080;"><label id="kwsTickerCountdownText1">test</label></td></tr>
<tr><td style="font-style:normal; font-family: Arial; font-size: x-large; font-weight: bold; font-variant: normal; color: #008080;"><label id="kwsTickerCountdownText2">test</label></td></tr>
<tr><td style="font-style:normal; font-family: Arial; font-size: x-large; font-weight: bold; font-variant: normal; color: #008080;"><label id="kwsTickerCountdownText3">test</label></td></tr>
</table>
</div>
</body>
<script language="javascript" type="text/javascript">
    var now = new Date(); //today
    var dob = new Date(2011, 2, 9); //date of birth
    var doc = new Date(2010, 5, 1); //start date of pregnancy
    var one_day = 1000 * 60 * 60 * 24; //milliseconds in one day
    var days_total = Math.ceil((dob.getTime() - now.getTime()) / (one_day)); //number of days left
    var weeks = Math.floor(days_total / 7); //number of weeks left
    var days = days_total % 7; //number of days in addition to the weeks left
    var days_gone_total = Math.ceil((now.getTime() - doc.getTime()) / one_day); //number of days gone
    var weeks_gone = Math.floor(days_gone_total / 7); //number of weeks gone
    var days_gone = days_gone_total % 7; //number of days in addition to the weeks gone
    var lab1 = document.getElementById('kwsTickerCountdownText1'); //get label 1
    var lab2 = document.getElementById('kwsTickerCountdownText2'); //get label 2
    var lab3 = document.getElementById('kwsTickerCountdownText3'); //get label 3
    var img = document.getElementById('kwsTickerCountdownImage'); //get image element
    var url = document.getElementById('kwsTickerCountdownUrl'); // get url element of image element
    lab1.innerHTML = (weeks_gone + 1) + 'th week';
    if (days_gone > 0) { lab2.innerHTML = weeks_gone + ' weeks and ' + days_gone + ' days on the way.'; } else { lab2.innerHTML = weeks_gone + ' weeks on the way.'; }
    if (days > 0) { lab3.innerHTML = 'Only ' + weeks + ' weeks and ' + days + ' days left!'; } else { lab3.innerHTML = 'Only ' + weeks + ' weeks left!'; }
    img.setAttribute('src', 'http://images.3dpregnancy.com/en/2D/200/' + weeks_gone + '-weeks-pregnant.jpg');
    url.setAttribute('href', 'http://3dpregnancy.parentsconnect.com/calendar/' + weeks_gone + '-weeks-pregnant.html');

    var tickerDiv = document.getElementById('tickerDiv');
    tickerDiv.style.width = screen.width;
    tickerDiv.style.height = screen.height;

    window.setTimeout("Tick()", 500, "javascript");
</script>
<script language="javascript" type="text/javascript">
    function Tick() {
        var tabl = document.getElementById('kwsTickerLayoutTable');
        var x = Math.floor(Math.random() * (screen.width - tabl.offsetWidth)) + 'px';
        tabl.style.left = x;
        var y = Math.floor(Math.random() * (screen.height - tabl.offsetHeight)) + 'px';
        tabl.style.top = y;
        window.setTimeout("Tick()", 2000, "javascript");
    }
</script>
</html>

If you want to have a background image, save a JPG-file called “scrback.jpg” into the same directory. If you don’t want a background image (if you are concerned about burn-in etc.), you may need to remove the following text: background-image:url('scrback.jpg') from the markup above.

Lastly, go into the screen saver options on your computer and configure the HTML file you created as the one you want your HTML screen saver to use.

Pregnancy ticker

The following HTML is a pregnancy ticker. The HTML fetches an image from http://www.3dpregnancyticker.com. Click on the image to go to the web site. To put in your own due date you will need to change the values for the “dob” and “doc” variables. It may be used as a desktop item on Windows. I have also used it on a HTML Screen Saver.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<div style="height:100%;width:100%;vertical-align:middle;text-align:center;background-color:MidnightBlue;">
<table id="kwsTickerLayoutTable" border="0" cellspacing="2" cellpadding="1" style="background-color:#FFEEFF;text-align:left;">
<tr><td rowspan="3" style="background-color:green;"><a id="kwsTickerCountdownUrl" href="" style="border:0;"><img id="kwsTickerCountdownImage" width="100px" alt="" src="" style="border: 1px solid #EEDDEE;" /></a></td><td style="font-style:normal; font-family: Arial; font-size: x-large; font-weight: bold; font-variant: normal; color: #008080;"><label id="kwsTickerCountdownText1">test</label></td></tr>
<tr><td style="font-style:normal; font-family: Arial; font-size: x-large; font-weight: bold; font-variant: normal; color: #008080;"><label id="kwsTickerCountdownText2">test</label></td></tr>
<tr><td style="font-style:normal; font-family: Arial; font-size: x-large; font-weight: bold; font-variant: normal; color: #008080;"><label id="kwsTickerCountdownText3">test</label></td></tr>
</table>
</div>
</body>
<script language="javascript" type="text/javascript">
    var now = new Date(); //today
    var dob = new Date(2011, 2, 9); //date of birth
    var doc = new Date(2010, 5, 1); //start date of pregnancy
    var one_day = 1000 * 60 * 60 * 24; //milliseconds in one day
    var days_total = Math.ceil((dob.getTime() - now.getTime()) / (one_day)); //number of days left
    var weeks = Math.floor(days_total / 7); //number of weeks left
    var days = days_total % 7; //number of days in addition to the weeks left
    var days_gone_total = Math.ceil((now.getTime() - doc.getTime()) / one_day); //number of days gone
    var weeks_gone = Math.floor(days_gone_total / 7); //number of weeks gone
    var days_gone = days_gone_total % 7; //number of days in addition to the weeks gone
    var lab1 = document.getElementById('kwsTickerCountdownText1'); //get label 1
    var lab2 = document.getElementById('kwsTickerCountdownText2'); //get label 2
    var lab3 = document.getElementById('kwsTickerCountdownText3'); //get label 3
    var img = document.getElementById('kwsTickerCountdownImage'); //get image element
    var url = document.getElementById('kwsTickerCountdownUrl'); // get url element of image element
    lab1.innerHTML = (weeks_gone + 1) + 'th week';
    lab2.innerHTML = weeks_gone + ' weeks and ' + days_gone + ' days on the way.';
    if (days > 0) { lab3.innerHTML = 'Only ' + weeks + ' weeks and ' + days + ' days left!'; }
    else { lab3.innerHTML = 'Only ' + weeks + ' weeks left!'; }
    img.setAttribute('src', 'http://images.3dpregnancy.com/en/2D/200/' + weeks_gone + '-weeks-pregnant.jpg');
    url.setAttribute('href', 'http://3dpregnancy.parentsconnect.com/calendar/' + weeks_gone + '-weeks-pregnant.html');
</script>
</html>