Pages

Friday 24 August 2012

What is use of Option Explicit statement?


·         The Option Explicit statement forces the explicit declaration of all variables using the Dim, Private, Public, ReDim, etc statements.
·         In a long program, this statement prevents the accidental reuse of the name of a previously declared variable. Also, if you mistype/typos a declared variable's name or try to use an undeclared variable, an error message is generated. 
·         By default the Option Explicit is On.
·         Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.

1) Option Explicit On Example:
Option Explicit On
Dim intVar As Integer
intVar = 10
intUndeclare = 10  'causes complie time ERROR due to 'intUndeclare' not declare and Option Explicit is On.

2) Option Explicit Off Example:
Option Explicit Off
Dim intVar As Integer
intVar = 10
intUndeclare = 10  Here ‘intUndeclare’ is not declared , because the Option Explicit Of, without any compiler error you can continue the program. 


Thursday 23 August 2012

Difference between Char, Varchar and Nvarchar in SQL?


Char DataType
Char datatype is used to store fixed length data. For example if we declared char(100) then it allocate memory for 100 characters, No matter how many length characters we insert/saved in it. Example : if we declare char(100) and insert/save only 10 characters of word then only 10 characters of memory will be used and other 90 characters of memory will be wasted.

Varchar DataType
Varchar, the name suggests itself that variable length characters. It is used to store non-unicode characters. It will allocate the memory based on number of characters inserted. Suppose if we declared varchar(100) it will allocates memory of 0 characters at the time of declaration. If we insert only 10 characters of word, it will allocate memory for only 10 characters.

Nvarchar DataType
Nvarchar datatype same as varchar datatype but only difference nvarchar is used to store Unicode characters and it allows you to store multiple languages in database. So when you are going to create bilingual or multi-language website then nvarchar is used.  




Saturday 11 August 2012

How to Copy and Paste block of code in visual studio?


Well we all know copy and paste function but if you want only some text to copy and paste from code block then it is very cumbersome process to do that one by one. 

Look at the following example:

Now suppose i want to change objUserEAL to objUserDetailsEAL in following code snippet then just press and hold down “ALT” key while selecting code region and you can copy/paste it anywhere you want in code.


Image of normal selection













Image of press and hold down “ALT” key while selection


















How to play alert/beep sound on console application.




For Console Application:

 Just used  "\a" for alert.
    Code:
    Console.Write("\a Your name please : ");
    string @MyString = Console.ReadLine();
    Console.Write(@MyString);














FOR WINDOWS APPLICATION:

Limitation : Only for *.wav files
Code:
private void playSimpleSound()
{
    SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\chimes.wav");
    simpleSound.Play();
}

















How to display additional information in design view.

In design view, user can show/hide additional information like tags (div, span, p, etc), this comes very handy when user want to make edit and add some additional contents in existing design view.

Steps to follow:
  •           Go to form “Design” mode.
  •          Click on View à Formatting Marks à Show.
Now you can show additional information display in following image and it becomes very useful for complex form design.



Friday 10 August 2012

Difference between Form.Show() vs Form.ShowDialog().


Forms and dialog boxes are either modal or modeless.

modal form or dialog box must be closed or hidden before you can continue working with the rest of the application.

A Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.

Modeless forms are harder to program, because users can access them in an unpredictable order. You have to keep the state of the application consistent no matter what the user does. Often, tool windows are shown in a modeless fashion.

Form.Show()
Form.ShowDialog()
It opens dialog box in modeless format.
It opens dialog box in modally format.
Ex:If we have two form like Form1 and Form2. If we open Form2 from Form1 than both form are remain active.
Ex:If we have two form like Form1 and Form2. If we open Form2 from Form1 than only Form2 remain active, we can't change/operate Form1 till Form2 closed.
If a form is shown as modeless, the code following the Show method is executed immediately after the form is displayed.
If a form is displayed as modal, the code following the ShowDialog method is not executed until the dialog box is closed.

Sunday 5 August 2012

How to prevent user to open multiple instance of an application?

If you were creating a Windows application, most often you would be doing long task like database creation, validation or some complex operation before form showing to user. so the patient user might wait few seconds to let the form show, others might launch the application again. To avoid user launch multiple instances of the same application, select the "Make single instance application" check box to prevent users from running multiple instances of your application as shown in below figure. The default setting for this check box is cleared (unchecked) so it allows multiple instances of the application to be run.

Steps to follow:

Right click on project --> Click on Properties --> Select Application Tab --> Check Make single instance application.







How to get rid from "ContextSwitchDeadlock was detected" exception.

ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x118b008 to COM context 0x118b178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

The contextSwitchDeadlock managed debugging assistant (MDA) is activated when a deadlock is detected during an attempted COM context transition.

Symptoms

  • The most common symptom is that a call on an unmanaged COM component from managed code does not return. 
  • Another symptom is memory usage increasing over time.
Resolution
To avoid these error please follow below steps and steps may vary slightly version to version. Following steps are taken from VS2010:

  1. Click on "Debug" menu from visual studio toolbar.
  2. Go to "Exception.." or just press shortcut key (CTRL+ALT+E).
  3. Select "Managed Debugging Assistants" Node.
  4. Uncheck "ContextSwitchDeadlock" from Thrown column.
  5. Click "OK" and enjoy.