Merge Datasets using C#
We can merge a dataset to another dataset using Merge. Lets assume we have two datasets ds1 and ds2.
DataSet ds1 = new DataSet();
da1.Fill(ds, "temp");
SQL CASE Expression
SQL CASE evaluates a list of conditions and returns one of multiple possible result expressions. It helps to provide multiple if conditions type of logic in our SQL Query.
Lets look in to an example :
Consider a Table Orders with some columns
1 2 3 4 5 6 7 8 | Declare Orders Table ( ID int identity (1, 1), Name Varchar(20), Quantity int, Code int, Value int ) |
Lets fetch records with columns ID, Name and Result where ID should be equal to 50. Here Result is obtained from Quantity, Code and value depending up on the Code. If Code is 0 Result is sum of Code+Quantity, If Code is 1 then Result is sum of Code+Value, else Result is Code+100 {if Code is neither 0 nor 1 then Result is Code+100} So depending up on the Code the Result value will be obtained.
Lets write a query which will do this
1 2 3 4 5 6 | SELECT ID, NAME, CASE WHEN CODE = 0 THEN CODE+QUANTITY WHEN CODE = 1 THEN CODE+VALUE ELSE CODE+100 END AS RESULT WHERE ID = 50 |
The above example shows how CASE Expression can be used to attain the records as per the requirement by using Multiple IF conditions, in this ELSE Clause is optional we can have one more WHEN ( WHEN CODE != 0 OR CODE !=1 THEN CODE+100)
Jagged Arrays C#
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an “array of arrays.” The following examples show how to declare, initialize, and access jagged arrays.
You have several rows of data, such as integers, and want to store them together in a single data structure. Jagged arrays are ideal for this.
A jagged array is declared by placing one pair of opening and closing brackets after another. With the initialization of the jagged array, only the size that defines the number of rows in the first pair of brackets is set. The second brackets that define the number of elements inside the row are kept empty because every row has a different number of elements.
The jagged array that i am working now contains three rows where the first row has two elements, the second row has six elements, and the third row has three elements. Got it!!
Jagged array looping is much [faster] than 2D array looping.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | int[][] jagged = new int[3][]; //Jagged Aray Declaration jagged[0] = new int[2] { 1, 2 }; jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 }; jagged[2] = new int[3] { 9, 10, 11 }; // So now lets use a for loop to iterate through the jagged array. <pre lang="csharp" line="1"> for (int row = 0; row <jagged.Length; row++) { for (int element = 0; element <jagged[row].Length; element++) { Console.WriteLine( "row: {0}, element: {1}, value: {2}", row, element, jagged[row][element]); } } // OUTPUT // ======== row: 0, element: 0, value: 1 row: 0, element: 1, value: 2 row: 1, element: 0, value: 3 row: 1, element: 1, value: 4 row: 1, element: 2, value: 5 row: 1, element: 3, value: 6 row: 1, element: 4, value: 7 row: 1, element: 5, value: 8 row: 2, element: 1, value: 9 row: 2, element: 2, value: 10 row: 2, element: 3, value: 11 |
Shut Down, Restart and Log Off PC using .NET, C#
Using .NET and C#.NET, We can perform Shut Down, Restart and Log off operation on current PC.
In .NET Framework we have a namespace Using System.Diagnostics which has the required class and methods to perform these operations from a .NET application running on a current PC.
Please Use the below buttons to perform Shutdown, Restart and Logoff on Current PC.
diagnostics.aspx.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //SHUT DOWN protected void btnShutDown_Click(object sender, EventArgs e) { Process.Start("shutdown.exe", "-s"); // By Default the Shutdown will take place after 30 Seconds //if you want to change the Delay try this one Process.Start("shutdown.exe","-s -t xx"); //Replace xx with Seconds example 10,20 etc } //RESTART protected void btnRestart_Click(object sender, EventArgs e) { Process.Start("shutdown.exe", "-r"); // By Default the Restart will take place after 30 Seconds //if you want to change the Delay try this one Process.Start("shutdown.exe","-r -t 10"); //Replace xx with Seconds example 10,20 etc } // LOG OFF protected void btnLogOff_Click(object sender, EventArgs e) { Process.Start("shutdown.exe", "-l"); //This Code Will Directly Log Off the System Without warnings } |
Difference between Convert.ToString() and .ToString() in C#
Well most of us might have seen Convert.TOString() and .ToString() in C# code. Baically both are used to convert a value to a String but there is a basic difference between them. When we have an NULL object, Convert.ToString(Object); handles the NULL value but where as Object.ToString(); does not handle the NULL value and it throws NULL Reference Exception. Lets go through an example:
1 2 3 4 5 6 7 8 9 10 11 | int age = 25; string s = age.ToString(); // age value will be converted to String string s = Convert.ToString(age); // age value will be converted to string // But when we have a null value for an object int age = 0; string s = age.ToString(); // age value will not be converted to String, //as .ToString(); will not handle the NULL values and it throws a NULL Reference Exception error. string s = Convert.ToString(age); // age value will be converted to string, //as Convert.ToString(); will handle the NULL values; |
So it is a Good Programming practise to use Covert.ToString() as it handles the NULL values which saves us from unnecessary debugging.
Difference between String and string in C#
For many C# newbies, it is a common confusion by seeing String and string. Both of them will be used as per individual preference and need.
String comes from a class System.String in .NET Framework where as string is an C# alias for System.String. As per the individual’s preference they use either String(Upper case) or string (lower case) as both work in the same way but not all the times.
We can use string do declare fields, properties etc that use the predefined type string, since the C# specification tells me this is good style. we can use String to use system-defined methods, such as String.Compare etc. They are originally defined on ‘System.String’, not ‘string’. ‘string’ is just an alias in this case.
1 2 3 4 5 6 7 8 9 10 11 12 13 | // can declare fields and properties. string name = "CSharp" // can not use Pre-defined methods string name = "CSharp"; name.Compare // possible to declare variables , properties.. String name = "CSharp"; //possible to use pre-defined methods String name = "CSharp"; name.Compare |
Similarly there are C# aliases for types in .NET Framework, int is an C# alias for Int32 which is a type in .NET Framework. In the same way there are C# aliases for types in .NET Framework.
Object Class, System.Object;
Object Class is the root for all the classes in the .NET Framework class hierarchy. Object Class is the ultimate base class from which all the classes are derived in .NET Framework.
Access Modifiers in C#
Access modifiers determines the accessibility level of
