Wednesday, October 14, 2009

Show hidden files option not getting retained

Why is "Show hidden files" option  not getting retained?
This is because, the update is not getting saved in registry because of the effect of virus. This can be fixed easily by following the steps below.


  1. Type regedit in Run command
  2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\SHOWALL
  3. Delete the entry CheckedValue in the right pane by right clicking on the entry and click delete.
  4. Create a new DWORD value entry by right clicking and clicking New-> DWORD value
  5. Change the name of new dword to CheckedValue
  6. Right click on the CheckedValue entry, hit modify and change value to 1. Hit OK.
  7. Thats all. The problem is solved.

Double click not working on drives.

This problem is because of the effect of virus which is very easy to fix. A file named autorun.inf might be created in the root directory of each drives. Deleting those files will fix the problem. Follow the steps below.

  1. Type cmd in Run window.
  2. If the drive is not C, type C: in command prompt
  3. Type cd\ to move to the root directory
  4. Type "attrib -r -h -s autorun.inf"
  5. Type "del autorun.inf"
  6. Move to next drive by typing D:
  7. Repeat the above steps(4 to 6) for all the drives.
  8. Restart the system.

Saturday, October 10, 2009

The changed values of a read only or disabled text box is not retained after postback

ScenarioThe changed values of a read only or disabled text box is not retained after postback.
Reason : Browser is not maintaining the state of a read only or disabled control.
Solution: The values of all the form controls are maintained inside Request.Form. So include the following code snippet inside page load to retain the values dynamically in the textbox.
Inside Page_Load
if(IsPostBack)
TextBox1.Text = Request.Form(this.TextBox1.ClientID);

Removing Yahoo Messenger from StartUp

For removing any item from start up, the normal practice is to remove the item from startup option in msconfig. For that do the following steps.


  1. Type msconfig in run command. (Admin privilege is required)
  2. In the configuration window that is popped up, select the startup tab.
  3. Uncheck the applications you want to remove from start up.
  4. Click Ok.
  5. From the next reboot onwards, the application wont start on startup.
For removing yahoo messenger, the above approach alone wont work. Do this extra stuff for accomplishing that.


  1. Login to Yahoo Messenger.
  2. Select  Messenger Menu Item. 
  3. Select Preferences.
  4. Select General Category.
  5. Uncheck automatically start yahoo messenger option. 
  6. Click OK.





Yahoo Messenger Preferences


If both these approaches are not working, follow the third and final approach. This is by removing the entry from registry as explained below.

  1. Stop yahoo if already running. Thia can be done by right click yahoo icon in taskbar and click exit.
  2. Take registry editor. This can be done by typing regedit in Run command.
  3. Navigate to the node "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
  4. Delete the entry Yahoo! Pager. (It is advisable to keep a back up of the registry entry before making any changes. This can be done by right clicking on Run Node and select export)
  5. Restart system.

Thursday, October 8, 2009

Clearing an ajax combobox in ASP.Net

An ajax combo-box  coming with 3.5 framework is not working the expected way when it comes in rebinding the combo-box and clearing the selection value. The normal combo-box.ClearSelection() wont work in this case. It is because the combo-box selection value is saved inside a hidden control which is not getting cleared. The above problem can be solved by including the following code snippet.


           combobox1.ClearSelection();
            foreach (Control control in combobox1.Controls)
            {
                if (control is HiddenField)
                    ((HiddenField)control).Value = "0"; //Where 0 is the value associated with the first selection item.
            }

Sunday, September 13, 2009

MSMQ Data Migration System



ASYNCHRONOUS MESSAGING THROUGH MSMQ in C#

1. OVERVIEW

Message queues provide an asynchronous communications protocol. The sender and receiver of the message do not need to interact with the message queue at the same time. Messages placed onto the queue are stored until the recipient retrieves them. These message queuing systems typically provide enhanced resilience functionality to ensure that messages do not get "lost" in the event of a system failure. Microsoft Message Queuing or MSMQ is a Message Queue implementation developed by Microsoft and deployed in its Windows Server operating systems since Windows NT 4 and Windows 95. 
MSMQ is essentially a messaging protocol that allows applications running on disparate servers to communicate in a failsafe manner. MSMQ enables communication across heterogeneous networks and between computers which may not always be connected. MSMQ is responsible for reliably delivering messages between applications inside and outside the enterprise. MSMQ ensures reliable delivery by placing messages that fail to reach their intended destination in a queue and then resending them once the destination is reachable. It also supports security and priority based messaging. Dead letter queues can be created for looking at messages which timed out or failed for other reasons. MSMQ also supports transactions. It permits multiple operations on multiple queues, with all of the operations wrapped in a single transaction, thus ensuring that either all or none of the operations will take effect.


2. A DATA MIGRATION SYSTEM INCORPORATING MSMQ AND HTTPLISTENER.

This case study deals with a data migration system which migrates data between application developed in java and dotnet. It a system to synchronise the data between the two systems. This example deals with a RoomType object (user business) created in 'Java' system to be migrated to the 'Dotnet' system. Once a room type is modified in one system, a message in standard XML format is sent to the other system. This case study deals with the processing of messages once it reaches the application developed in Dotnet.


The basic architecture of the data migration system is as shown below.


Fig1 : Block Diagram of MSMQ Data Migration System
  1. Incoming message from system1(java).
  2. Acknowledgment for the message is send back.
  3. XML message is send to the parser.
  4. Parsed message is converted to entity(RoomType Entity in this scenario) and is send to MSMQ.
  5. MSMQ Listener keep on watching the queue.
  6. Once a message reaches MSMQ, it is being sent to the MSMQ Listener.
  7. Listener invokes methods in the Business layer to insert the RoomType entity into database.
  8. Response is being sent to listener from Business Layer.

3. TECHINAL OVERVIEW

The main classes that are used in this sample are

  1. System.Net.HttpListener
  2. System.Xml.XMLReader
  3. System.Messaging.MessageQueue
  4.  
    3.1. HTTP Listener

    Using the HttpListener class, you can create a simple HTTP protocol listener that responds to HTTP requests. The listener is active for the lifetime of the HttpListener object .


    To use HttpListener, create a new instance of the class using the HttpListener constructor and use the Prefixes property to gain access to the collection that holds the strings that specify which Uniform Resource Identifier (URI) prefixes the HttpListener should process.


    A URI prefix string is composed of a scheme (http or https), a host, an optional port, and an optional path. An example of a complete prefix string is “http://localhost:8080/DataMigration/”. Prefixes must end in a forward slash (“/”). When a port is specified, the host element can be replaced with “*” to indicate that the HttpListener accepts requests sent to the port if the requested URI does not match any other prefix. Similarly, to specify that the HttpListener accepts all requests sent to a port, replace the host element with the “+” character, “https://+:8080”.


    To begin listening for requests from clients, add the URI prefixes to the collection and call the Start method. HttpListener offers both synchronous and asynchronous models for processing client requests. Requests and their associated responses are accessed using the HttpListenerContext object returned by the GetContext method or its asynchronous counterparts, the BeginGetContext and EndGetContext methods.


    In either model, incoming requests are accessed using the HttpListenerContext.Request property and are represented by HttpListenerRequest objects. Similarly, responses are accessed using the HttpListenerContext.Response property and are represented by HttpListenerResponse objects.


    The following code example demonstrates using a HttpListener which listens at port 8080.

    HttpListener listener = new HttpListener();
    listener.Prefixes.Add("http://*:8080/");
    listener.Start();
    Console.WriteLine("Listening...");
    while(true)//For continuous polling of http requests
    {
    // Note: The GetContext method blocks while waiting for a request.
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    Stream inputStream = request.InputStream;
    /*******************
    * XMLParser.ParseXML method which is explained in section 2.1 is invoked here. Data read from the Inputstream is the input to the ParseXML method. ParseXML method returns a RoomType entity. The RoomType entity is then pushed to the MSMQ.
    ********************/
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response.
    string responseString = " Response Message";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    output.Close();
    }
    listener.Stop();

    Code Snippet 1: Http LISTENER




    3.2. XML Parser

    This section deals with converting the XML message received by the HTTP Listener into a Entity, say RoomType Entity. XMLReader and XMLTextReader classes are used to navigate through each of the nodes in the XML. The text and the attribute value read form the XML is assigned to the RoomType entity.

    The logic behind the parsing is that each of the nodes are converted into a full element name.


    ie. Consider the following XML:






    Then each of the nodes become :
    roomtypes/roomtype/@name, value = DELUXE
    roomtypes/roomtype/@maxOccupancy , value=5
    roomtypes/roomtype/@minOccupancy, value = 3


    These sets of full element name and values are passed into a ParseMessage method which assigns the values to the entity as shown below(Code Snippet 4). The below code snippet converts the incoming request into a XMLReader object


    XmlTextReader textReader = new XmlTextReader(new MemoryStream (System.Text.UTF8Encoding.UTF8.GetBytes(request)));
    XMLReaderSettings settings = new XMLReaderSettings();

    settings.ValidationType = ValidationType.None;
    XMLReader reader= XMLReader.Create(textReader,settings);

     Code Snippet 2: Converting incoming request to XmlReader object


    while(reader.Read()) 
    {
       switch(reader.NodeType)
      {
       case XmlNodeType.Element:
         /*
         Business to format the node
         */
         ParseMessage(formattedNode, value);
       break;
       case XmlNodeType.Text:
         ParseMessage(formattedNode,value);
         break;
       }
    }

    Code Snippet 3: Navigating through Nodes in the XMLReader


    public void ParseMessage(String elementname, string value)
    {
       switch( elementname )
       {
         case roomTypes/roomtype/@name:
           roomType.Name= value;
         break;
         case roomTypes/roomtype/@maxoccupancy:
           roomType.MaxOccupancy= value;
         break;
         .
         .
       }
    }
    Code Snippet 4: Assigning values into Entity(e.g: roomType Entity)


    3.3. Queue Handler : Programming Message Queuing

    Message Queuing 3.0 is a part of Windows Server 2003 and Windows XP, Windows 2000 comes with the Message Queuing 2.0, which did not have support for the HTTP protocol and multicast messages. You can install a message queue in Windows XP using Add or Remove Programs, a separate section within windows components where Message Queuing options can be selected. Within the message Queuing options, various components can be selected (refer Fig 2 and Fig 3).



    Fig 2: Installing Message Queue






    Fig 3: Message Queue Components




    3.3.1. Creating a Message Queue

    In C#, we can create Message Queues programmatically using the Create() method of MessageQueue class. With Create() method, the path of the new queue must be passed. The path consists of the hostname where the queue is located and the name of the queue
      using (MessageQueue queue = MessageQueue.Create(@". \myqueue"))
         {
           queue.Label = "RoomType";
           Console.WriteLine("Queue Created:");
         }
    Code Snippet 5: Creating a Queue


    3.3.2. Sending a Message

    By using the Send method of the MessageQueue class, you can send the message to the queue. The object passed as an argument of the Send() Method is serialized queue.


    The message received by the HttpListener(3.1) is parsed by the XMLParser(3.2). The parsed message is converted to the RoomType Entity. Room Type entity is the input to the Send method.

    try
    {
    if (!MessageQueue.Exists(@".\Private$\FirstQueue"))
          MessageQueue.Create(@".\Private$\FirstQueue");


    MessageQueue queue = new MessageQueue(@".\Private$FirstQueue");
    queue.Formatter= new System.Messaging.XmlMessageFormatter(new string[]{"RoomType”});
    queue.Send("RoomType ", objRoomType);
    }
    catch (MessageQueueException ex)
    {
    Console.WriteLine(ex.Message);
    }

    Code Snippet 6: Sending entity to queue


    You can view the message in the Component Management admin tool as shown in figure 4. By opening the message and selecting the body tab of the dialog, you can see the message was formatted using XML.




    Fig 4: Viewing MessageQueue in the Component Management admin tool




    3.3.3. Receiving a Message : MSMQ Listener

    This is the code that is to be used in the MSMQ Listener page load. This would start the MSMQ listener. BeginReceive, and EndReceive(IAsyncResult) methods provide ways to asynchronously read messages from the queue.


    queue.Formatter = new XmlMessageFormatter(new Type[]{typeof(RoomType)});
    queue.ReceiveCompleted += new ReceiveCompletedEventHandler(MessageArrived);
    queue.BeginReceive();


    Code Snippet 7: Starting a MSMQ Listener


    public static void MessageArrived(object source,ReceiveCompletedEventArgs e)
    {
       MessageQueue queue = (MessageQueue)source;
       Message message = queue.EndReceive(e.AsyncResult);
       if(message.Label.Equals(“RoomType”)
       {
          RoomType objRoomType = (RoomType)message.Body;
         /*
         Business to process the object received from Queue
         */
    }
    queue.BeginReceive();
    }

    Code Snippet 8: Event handler for MSMQ Listener