Wednesday, November 25, 2009

exit for in C#

If you wanna use something similar with “exit for”  in Visual Basic, in C# you can just use break.

 

For example:

                        foreach (XmlNode xmlChild in xmlDestNodes.ChildNodes)

                        {

                              if (sDestCode == xmlChild.Attributes["code"].InnerText)

                              {

                                    sDestination = xmlChild.InnerText;

                                    break;

                              }

                        }

 

So easy, isn’t it?

Thursday, November 12, 2009

Error Handling using C#

Try, Catch and Finally

 

Build error – the compiler tells you what line contains a build error and provides useful information

Runtime error – this type can crash your application if not properly handled.

 

Visual C# supports structured exception handling

 

Block Types for error handling

Try – block where the error can occurs

Catch – block where you handle the exception

Finally – block that will be execute after try or catch block will be complete.

 

Try forms:

-          try + one or more catch

-          try + finally – if you omit the catch session, the error will be ignored (= “On error resume next”?)

-          try + one or more catch + finally.

 

 

Exception object - represents errors that occur during application execution.

 

System.Diagnostics – this namespace provides classes that allow you to interact with system processes, event logs and performance counters. Event log, Process and Performance counter are classes from it.

It also provides classes that allow you to debug your application and trace the execution of your code. See Trace and Debug Classes.

 

Debbugging Your Code

Visual C#  throws an exception when a runtime error occurs. If this exception is not handled, the program is finished.

In Visual Studio, breakpoints are saved with the project. You don’t have to reset all your break points each time you open the project.

 

Action That Can be Taken at a Break Point

 

Action

Keystroke

Description

Continue Code Execute

F5

Continues execution at the current break statement

Step Into

F11

Executes the statement at the break point and then stops at the next statement. If the current statement is a function call, F11 enters the function and stops at the first statement in function.

Step Over

F10

Executes the statement at the break point and then stops at the next statement. If the current statement is a function call, the function is run in its entirety; execution stops at the statement following the function call.

Step Out

Shift + F11

Runs all the statements in the current procedure and halts execution at the statement following the one that called the current procedure.

Stop Debugging

Shift + F5

Stops debugging the project and returns to design mode.

 

Only Visual Studio 2008 supports on-the-fly code editing – you can modify code while debugging it.

 

Output window – Visual C# uses the Output window to display various status messages and build errors. The most useful feature of the Output window, is the capability to send data to it from a running application.

 

To print data to the Output window, use the WriteLine() method of the Debug object, like this:

 

System.Diagnostics.Debug.WriteLine(“Results = “ + lngResults);

 

Whatever you place withing the parentheses of the WriteLine() method is what is printed to the Output window. Note that you can print literal text and numbers, variables, or expressions. WriteLine() is most useful in cases where you want to know the value of a variable, but you don’t want to halt code execution using a break point. For instance, suppose you have a number of statements that manipulate a variable. You can sprinkle WriteLine() statements into the code to print the variable’s content at strategic points.

 

Just remember that the Output Window isn’t available to a compiled component; calls to the Debug object are ignored by the compiler when creating distributable components.

 

 

Wednesday, September 16, 2009

How do I rename a column using SQL Server?

It's very easy to rename a column using SQL Server:

sp_rename 'table.old_column_name', 'new_column_name'

very simple, isn't it?