Saturday, 11 January 2014

Different techniques to move data from one webform to another

1. Cross Page Postback

2. Context.Handler object

3. Query strings

4. Cookies

5. Session state

6. Application state

Points to remember about query strings

1. Querystrings are name/value collection pairs

2. Using querystrings, is a very comman way to send data from one webform to another.

3. Query strings are appended to the page URL.

4. ?(Question Mark), indicates the beginning of a query string and it's value.

5. It is possible to use more than one query string. The first query string is specified using the ?(question mark). Subsequent query strings can be appended to the URL using the &(ampersand) symbol.

6. There is a limit on the Query string length. Hence, Query strings cannot be used to send very long data.

7. Query strings are visible to the user, hence should not be used to send sensitive information, unless encrypted.

8. To read the query string value, use Request object's QueryString property.

9. &(ampersand) is used to concatenate query strings, so if you want to send &, as value for the query string there are 2 ways, as shown below

Using Server.UrlEncode() method

Response.Redirect("WebForm2.aspx?UserNam­e=" + Server.UrlEncode(txtName.Text) + "&UserEmail=" + Server.UrlEncode(txtEmail.Text));

Or

&(ampersand) is encoded as %26, so use, Replace() function to replace & with %26

Response.Redirect("WebForm2.aspx?UserNam­e=" + txtName.Text.Replace("&", "%26") + "&UserEmail=" + txtEmail.Text.Replace("&", "%26"));

Tuesday, 7 January 2014

Encapsulation & Abstraction

Two concepts that go together in the object oriented approach are Encapsulation & Abstraction. Abstraction is the representation of only the essential features of an object, while Encapsulation is the hiding of the non-essential features.

Think of a person driving a car. He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation). Instead, he needs to know things such as how much turning the steering wheel needs, etc (Abstraction).

It is used to display only necessary and essential features of an object to ouside the world.Means displaying what is necessary and encapsulate the unnecessary things to outside the world.Hiding can be achieved by using "private" access modifiers.

Encapsulation enables a class to hide the internal implementation details and to protect itself from unwanted changes that would result in an invalid or inconsistent internal state. For that reason, encapsulation is also sometimes referred to as data hiding.

As an example of encapsulation at work, think about your car. You start your car in the morning by inserting a key and turning it (or simply pushing a button, in some cases). The details of what happens when you turn the key (or push the button) that actually causes the engine to start running are hidden from you. You don't need to know about them to start the car. It also means you can't influence or change the internal state of the engine except by turning the ignition key.

By hiding the internal details and data, you create a public interface or abstraction representing the external details of a class. This abstraction describes what actions the class can perform and what information the class makes publicly available. As long as the public interface does not change, the internal details can change in any way required without having an adverse affect on other classes or code that depends on it.

By keeping the public interface of a class small and by providing a high degree of fidelity between your class and the real-world object it represents, you help ensure that your class will be familiar to other programmers who need to use it.

Let's look at our car example again. By encapsulating the details of what happens when you start your car and providing an action, StartCar, and information, such as IsCarStarted, we have defined a public interface, thereby creating an abstraction (or at least a partial abstraction, because cars do much more than just start) of a car.

Thursday, 2 January 2014

Windows Service development questions

1. What are Windows services?

Windows services, previously known as NT services, are applications that are installed on the system as system services. In other words, Windows services are applications that run in the background with the Windows operating system. The primary use of Windows services is to reduce the consumption of memory required for performing backend operations. Let's take an example to understand this easily.

Suppose you want to perform a variety of functions, such as monitor the performance of your computer or application, check the status of an application, and manage various devices, such as printers.

In such a case, you can use Windows services to reduce memory consumption. In addition, Windows services can run on your system even if you have not logged on to your computer. In addition, these services do not have any user interface.

2. Can you share a process between Windows services?

Yes, you can share a process between Windows services.

3. In .NET, which is the parent class to create all Windows services?

The ServiceBase class is the parent class to create all Windows services.

4. Which class in .NET is used to install a Windows service?

The ServiceInstaller class, also known as the project installer class, is used to install a Windows service.

5. While installing a Windows service, an EventLogInstaller class is automatically created to install the event log related to the particular service. Is it true?

Yes, it is true.

6. Which property of the ServiceBase class can be used to specify whether a service can be paused and resumed?

The CanPauseAndContinue property provides such type of service.

7. How to find out if a service present in different machine has been stopped or started

Use ServiceController Class present in System.ServiceProcess namespace to determine the status of the service. Have a timer in the application which is monitoring the service. The timer should poll service to determine its status.

ServiceController c = new ServiceController("", "");

8. Does windows service pick up any changes made to app.config when its running?

No. The service needs to be restarted to pick up any new changes made to the config file.

9. Debug a windows service

Below are the three methods

1.Create a new test class. Then call the functions from test class.

2.Add a line System.Diagnostics.Debugger.Break(); wherever you want to debug.At runtime a new instance will be created and debugging can be carried out in separate windows.

10. How to install a windows service

Go to visual studio command prompt. Navigate to path where the exe that you want to install is present.

To install, type

installutil projectname.exe

To uninstall, type

installutil /u projectname.exe

11. Can we pass values to windows service? If yes, how?

Yes, we can pass values to Windows Services

1) If you want to pass values at service startup, yo can send the values as command-line parameters. And (string[] args) of OnStart method will catch these parameters.

protected override void OnStart(string[] args) { }

2) If you want to pass values while service is running, you required to implement IPC (Interprocess communication) in Windows Service.

You can implement IPC by using any of following .NET technologies like NamedPipes, MSMQ, Remoting, TCP/IP Client/Service Programming or WCF Self Hosting Service.