Site Checker in C#–A Website health monitoring system

January 19th, 2012
Comments Off

Below code checks the list of websites and sends a notification mail if found if any of the website is down. This I wrote for a special purpose but thought to share as this might be a helpful code snippet for small to medium companies.

You can see many places in the code are hard coded and exceptions not handled properly since I wrote this for a special purpose and I am not sharing the complete code here. So please feel free to modify.

Store all the domains in a text file (I used sites.txt) and keep in the same folder as .exe. Put one domain per line. “http://” not required.

 public bool ErrorFlag = false;
        public StringBuilder SiteString = new StringBuilder(string.Empty);
 
 
        private void btnCheck_Click(object sender, EventArgs e)
        {
            CheckSites();
        }
 
 
        private void CheckSites()
        {
            string sitestatus = "Ok";
            foreach (ListViewItem lvi in listView1.Items)
            {
                lvi.UseItemStyleForSubItems = false;   
                lvi.SubItems.Add("...");
 
		// Use some threading methods instead of DoEvents()
                Application.DoEvents(); 
 
                WebRequest request = WebRequest.Create(lvi.Text);
                HttpWebResponse response;
 
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch
                {
                    response = null;
                }
                if (response == null || 
			response.StatusCode != HttpStatusCode.OK)
                {
                    SiteString.Append(" - " + lvi.Text + "\n");
 
                    lvi.ForeColor = lvi.SubItems[1].ForeColor = Color.Red; 
                    sitestatus = "Error";
                    ErrorFlag = true;
                }
                else
                {
                    response.Close();
                    lvi.SubItems[1].ForeColor = Color.Green;    
                    sitestatus = "Ok";
                }
 
                lvi.SubItems[1].Text = sitestatus;
 
 
            }
 
            if (ErrorFlag)
            {
                StringBuilder message = new StringBuilder();
 
 
                if (SiteString.Length > 0)
                {
                    message.Append("Below website(s) may need your/IT 
			attention:\n\n");
                    message.Append(SiteString.ToString());
                }
                else
                {
                    message.Append("SiteChecker application failed to 
			load the sites.txt file. Please inform IT person");
                }
                message.Append("\n\n\nThis is an automatic 
			notification from SiteChecker applicaiton.\n");
                message.Append("[Application created by PraVeeN 
				under C# WinForms on VS Express 2010]");
 
                SendMail(message.ToString());
 
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            IEnumerable<string> sites;
            try
            {
                sites = File.ReadLines("sites.txt");
                foreach (var site in sites)
                {
                    listView1.Items.Add("http://" + site + "/");
                }
            }
            catch
            {
                ErrorFlag = true;
            }
 
	    //Application.DoEvents();
	    //CheckSites();
	    //Application.Exit();
        }
 
        private void SendMail(string content)
        {
		// Mail sending procedure
	}
VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

C#, Code Snippets

Visual Studio 11 and .NET Framework 4.5 previews

January 9th, 2012
Comments Off

Don’t be late. Check out what’s special in new versions.

URL: http://msdn.microsoft.com/en-US/vstudio/hh127353

Here is a what’s new link for .net framework 4.5.

VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

DOTNET, News

Programmability Enhancements in SQL Server 2012

January 6th, 2012
Comments Off

Programmability enhancements in the Database Engine include FileTables, statistical semantic search, property-scoped full-text search and customizable proximity search, ad-hoc query paging, circular arc segment support for spatial types, support for sequence objects, default support for 15,000 partitions, and numerous improvements and additions to Transact-SQL.

MSDN page here – http://msdn.microsoft.com/en-us/library/cc645577(v=SQL.110).aspx

VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

SQL

My new year resolutions for 2012

January 1st, 2012
Comments Off
  • ITIL Certification – Information Technology Infrastructure Library
  • Be active in Toastmasters Club
  • Learn Arabic, if I continue in UAE
  • Learn new technologies – as usual
  • Continue with next papers of MBA
  • More MCP certifications
VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Personal

jQuery: Get sort order/position after re-ordering

December 27th, 2011
Comments Off
	var gadgets_sortorder;
	$(function() {
		$( "#sortable" ).sortable({
			revert: true,
 
			update: function(event, ui) {       
				gadgets_sortorder = $(this).sortable('serialize');  
			}
 
		});
 
		$( "ul, li" ).disableSelection();
	});
 
 
	function SavePos() {
 
		alert(gadgets_sortorder);
	}
VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

ASP.NET, Code Snippets, Web Design

I am PMP – Project Management Professional now!

December 6th, 2011
Comments Off

Yes, from today onwards I am a certified or ‘qualified’ Project Manager

 

image

VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Personal

Passed Brainbench Project Management

November 25th, 2011
Comments Off

Congratulations! You passed at the Master’s Level”

“You have now earned a Brainbench Master Certification in Project Management (2008), which is valid for 3 years from today’s date.”

Strong Areas

  • Project Control
  • Project Communications
  • Project Management Concepts
  • Project Schedule
  • Project Planning
  • Project Characteristics

Weak Areas

  • None noted
VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

General

Don’t Hardcode

November 9th, 2011
Comments Off

Even though hardcoding is a bad practice, I usually forgive to junior resources (1-2 years experience) until me or somebody catch them while code review. It becomes a sin if is done by experienced programmers. Always go for generic, flexible, scalable architectures (whether it is database or programming) when ever possible. Hard coding might give you a quick output but when the requirement changes you will require three times higher time for maintaining it.

Today’s customers expect flexibility so you may not be able to say NO to changes.

Suppose you have three categories “General”, “Exotic” and “Fresh”. For a hard coding approach. You might design something like:

-----------------------------------------
|  id | name | general | exotic | fresh |
-----------------------------------------

You will add one more column if you have to add one more category. Rather than editing the table schema, cant you make a fixed table like this?

-------------------------
|  id | name | category |
-------------------------

Entity-attribute-value or EAV model will be a good catch for these kinds of requirements.

VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: -2 (from 2 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Architecture, General

Microsoft Announces Windows Phone Developer Program for India

November 3rd, 2011
Comments Off

At the recent Mobile Developer Summit which took place on November 2, 2011 at Bangalore Microsoft announced the details of the developer programme for Windows Mobile in India with ‘I unlock Joy’, a unique Application Development and Submission Program, where technology professionals and students get an opportunity to develop applications and showcase creativity.

‘I unlock Joy’ programme for developers has started from November 1, 2011 and will continue till June 30, 2012 whereas the program for students will begin on December 18, 2011 and conclude on March 30 next year.

Developer competitive categories:

· “SUBMIT & WIN” CATEGORY: A registered participant submits THREE qualified Windows Phone Application in the Marketplace and gets a chance to win a Windows Phone. In this category, the Applications must be distinct and of non-trivial utility value for the Application User. A “Spam” or close replicas of existing Windows Phone Applications are disqualified.

· “PORT 2 APPS & WIN” CATEGORY: A registered participant submitting TWO qualified Windows Phone Applications in the Marketplace which are ported from existing Android or iPhone Applications get to win a Windows Phone.

· “WOMEN SPECIAL” CATEGORY: A first of its kind in the developer space. The first 100 registered Woman Developers who submit ONE (1) qualified Windows Phone Application each in the Marketplace qualify to win a Windows Phone each. The applications can be based on Entertainment, Fashion, Leisure, Sports, Recreation or Travel themes.

Student competitive categories:

· “STUDENT” CATEGORY: Calling on the community to be a part of the initiative, the “I unlock Joy” programme is customized to unfold their innovative and creative best and become among the select first few proud owners of the Windows Phone.

The contestants need to build one or more applications using Dev Tools and submit on AppHub. If their apps get certified and published on the Windows Phone Marketplace during the contest period which ends November 18, they can claim goodies like a brand new Windows Phone, certificate of acknowledgment from Microsoft and Windows Phone T-Shirts as well as USB flash drives.

VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.11_1134]
Rating: -1 (from 1 vote)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

News

Get exchange rate from foxrate.org without using XML-RPC calls

October 30th, 2011
Comments Off

http://foxrate.org/ is a webiste offers exchange rate details using XML-RPC calls. You an see the schema in the home page itself. Below is a C# code I demonstrate which fetches the exchange rate without any RPC calls or using any XML-RPC libraries.

Note that, this code is as on 30th Oct 2011. Why I say you is because schemas are subject to change without notice and if the schema got changed at the time of your visit to this blog, below script wont work. But still, minor tweaks will help you make it work. I bet.

 private string GetExchangeRate(string frmCurr, string toCurr, double rate)
        {
 
            WebRequest request = WebRequest.Create("http://foxrate.org/rpc/");
 
            request.Method = "POST";
 
            string postData = @"<?xml version='1.0'?>
            <methodCall>
                       <methodName> foxrate.currencyConvert </methodName>
                <params>
 
                    <param>
                        <value>
                            <string>[frmCurr]</string>
                        </value>
                    </param>
 
                    <param>
                        <value>
                            <string>[toCurr]</string>
                        </value>
                    </param>
 
                    <param>
                        <value>
                            <double>[rate]</double>
                        </value>
                    </param>
 
                </params>
            </methodCall>
        ";
            postData = postData.Replace("[frmCurr]", frmCurr.ToString())
                .Replace("[toCurr]", toCurr.ToString())
                .Replace("[rate]", rate.ToString());
 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
 
            WebResponse response = request.GetResponse();
 
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
 
            reader.Close();
            dataStream.Close();
            response.Close();
 
            var xdoc = XDocument.Parse(responseFromServer);
            var data = xdoc.Descendants("struct")
                .Elements("member")
                .Elements("value")
                .Elements("double").First().Value;
 
            return data;
 
        }

You can make the call using. Eg:

textBox2.Text = GetExchangeRate("USD", "INR", 1);
VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: 0 (from 2 votes)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

C#, Code Snippets