Jun 22, 2011

Enable/Disable LAN interface by command prompt in Windows XP

First thing you need to check "devcon" is working in your XP version.
Go to command prompt and type

If it gives an error, download this utility (an exe) from microsoft website.

Extract that into a known place and change your command prompt path to that folder where the .exe file exist.

Now if you type condev it should give a help details.

Now you need to get the Ethernet name from that command. To do that type;

C:\devcon_folder> devcon hwids "PCI\*"  >pcidevname.txt

This will print all PCI devices hardware details into that text file. So go through that text file and get the ID of your ethernet card. (Use Ctrl+F and key word "Ethernet")

It will be like this.

    Name: Realtek RTL8139/810x Family Fast Ethernet NIC
    Hardware ID's:
    Compatible ID's:
        PCI\VEN_10EC&DEV_8139 ......

Get your ID equal to "PCI\VEN_10EC&*"

Then type to disable;

C:\devcon_folder>devcon disable "PCI\VEN_10EC&*"

To enable

C:\devcon_folder>devcon enable "PCI\VEN_10EC&*"

Jun 18, 2011

Why do we need two address types to communicate via a network?

Think that we need to go to USA, from Sri Lanka. But assume there is no direct flight to USA. So what we will do…? If there are direct flights from Sri Lanka to Singapore and from Singapore to USA, we can go to USA. In such a scenario we have two types of destinations. One is a long destination (LD) which is from Sri Lanka to USA and the other is short destinations (SD) which are Sri Lanka to Singapore and Singapore to USA. If we can achieve all the short destinations in a correct order, ultimately we can reach to our long destination. So, when we are in Sri Lanka LD is SL-USA but SD is SL-SIN. Then we travel to Singapore. In Singapore LD is SL-USA but SD is SIN-USA. Now we are in USA. Note that when we reach to Singapore we change the SD but we haven’t change the LD, because our ultimate goal is to go to USA.

Click on the image to maximize

A collection of interconnected devices are called as a network. When the number of devises gets bigger and bigger, the single network will divide into manageable sub networks. In a network all devices are recognized by its MAC (Media Access Control) address. IP address is a logical address that assigned to device in a way that human can remember, but it’s limited to a single network. The network is identified by the subnet mask. Default gateway is used as an access point to go to another network.

Think you are using the C1 Computer. You need to copy a file in S1 ( server. So how C1 will communicate with S1? To do a copy you need to specify an IP address of the server. In this scenario it is When you enter that IP address your machine will tally it with its IP address and the subnet mask. C1s subnet mask is, so its network address is The requested IP is not in the range of –, or else its no in the same network. Then your computer will try to move the request to the default gateway which is specified as

Computer will broadcast a ARP message to the network and only the corresponding interface will reply for that. Now the computer knows the MAC address of the default gateway. So it will create a packet with Source IP (S-IP), Destination IP (D-IP) as Source MAC address (S-MAC) is aaaa:aaaa:aaaa . But C1 doesn’t know the MAC address of the S1, and its now decided to forward the packet to the default gateway. Therefore, destination MAC address (D-MAC) will be bbbb:bbbb:bbbb.
Now the packet is in the R1 router. It will check the packets D-MAC address and feel it’s for the router R1. When it checks the D-IP address router realize that it’s not for the router but it need to go through the router to some were else. Now the router checks its routing table and realizes that the required network is not connected to it. But the router is capable of knowing neighbor route; therefore R1 will try to move the packet to the next best router. To do that R1 modify the packets S-MAC to cccc:cccc:cccc and D-MAC dddd:dddd:dddd. But it wont change the S-IP (but NAT will do a small change) and D-IP.
So this is like we travelling form SL to SIN in the example. This process will happen in all the routers which are in the path for the network

When the packet comes to R3, it also checks the D-MAC and D-IP. When R3 checks the D-IP, it knows that it has connected to that requested network. So it moves the packet to the switch which will map D-IP to its MAC and forward it to the S1. So in this whole process MAC works as a temporary address, but IP works as the permanent address. So that’s why we need two addresses to communicate over a network.

Jun 17, 2011

Add Syntax Highlighter To Blogger

We can use a syntax highlighter to display source code syntax in different colours. It will create a user friendly environment to read the source code.
I'm going to use google-code-prettify to do this task.

First thing you need to add a java script and a css to your blog. It should be placed after the <head> tag. To do this you must go to settings -> design -> edit html.
Press "Ctrl+f" and type " <head" , it will help to find the head tag. Then add the Javascripts and css like this.

<link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css' rel='stylesheet' type='text/css'/> 
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js' type='text/javascript'/>

Then you need to modify the "<body>" tag. It should be like this.

<body onload="prettyPrint()" ... >

Then you can write your code within <pre$gt;</pre$gt; or <code$gt; </code$gt;. But to add the colours you need to add the class as,

<pre class="prittyprint">
   <!-- Your code comes here. -->

Note: - First use "Compose" to add the coding then go to "Edit HTML" to add the pre tag with class. There is an effect between <> and &lt;&gt; .

This is a sample java program.

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.

Jun 16, 2011

Visual Studio and C# (Some Useful Code Samples)

Creating MDI Forms (Multiple Document Interface).

Add 2 new Forms (Form1, Form2) to the solute on. Form1 will be the main frame and Form2 will be inside the first one. So in the first one we must make it as a MDIParent form. To do that, select the Form1 and go to its properties. In that list you will see a property called “IsMdiContainer”. Set it to TRUE. Now you will see that the form has been changed. Then go to the code behind of the Form1. In the constructor of the class, you should write this code in order to view the Form2 inside the Form1.

Note: - You can use a toolStrip instead of writing the code in the constructor.
public Form1()
        InitializeComponent(); // This is an auto generated code.
        // start of our code.
        Form2 fm = new Form2();
        Fm.MdiParent = this;
        // end of our code.
These 3 lines of code will do the trick. Think if you need to open another form (Form3) by clicking a button in Form2, so you need to modify the 2nd line of this code to,
Fm.MdiParent = this.Parent;
This will set the MdiParent for Form3, by the parent of Form2. It means that it will use the Form1 as the parent of the Form3.

Connect your .NET Application to MySQL Database.

Most of the time .Net applications will use MSSQL database. But here is a sample code to connect your application to MySQL database.

First of all you need to install MySQL connecter for .NET environment. It can be freely download from this link. (For windows and MONO project.)


Note: - Close Visual Studio before you start installing the connector.

I will not going to create a DbAccess class here, just simply tell how to use it.

First of all you need to add a reference called “MySql.Data” to your project. Then you need to add namespace,
Using MySql.Data; 
After that I will use a button click event to get data from the MySQL database.
private void button1_Click(object sender, EventArgs e)
   string result = null;

   string ConString = "User Id=user;"+ 
                      "Persist Security Info=True;"+

   MySqlConnection conn = new MySqlConnection(ConString);

   MySqlCommand cmd = conn.CreateCommand();
   cmd.CommandText = "Select * from test";

   MySqlDataReader reader = cmd.ExecuteReader();
   while (reader.Read())
      result += reader["name"].ToString() + " \n ";

Use configuration files in Visual Studio.

In the previous sample code I hard code the MySQL connection string. But that is not a good practice. Because some configurations like the password will rapidly change. So if we hard code these settings, we need to compile the application each and every time after changing the password. So to avoid that overhead we use a configuration file called “App.Conf”. You can add this file to your project by going to add new item.

This is an XML file. So to add the MySQL connection string data, you need to modify this XML file into something like this.
<?xml version="1.0" encoding="utf-8" ?>
        <add name="MySQLConStr" 
             connectionString="User Id=user; 
                               Persist Security Info=True; 
             providerName="MySql.Data.MySqlClient" />

Then go to your C# code and modify the connection string to something like this. To do this you may need to add a namespace,
using System.Configuration;

string ConString = ConfigurationManager.ConnectionStrings["MySQLConStr"].ToString();
Note: - Give the same name to both <add name="" ... > and ConnectionString[""].