Monday 31 March 2014

Why C# is a Type-Safe Language

"It is impossible to write unsafe code in C#". Some programmers think that this statement is not 100% correct but it is correct and C# is definitely a type-safe language.

C# enforces its variable types very strongly for ex: this means that C# is always aware that an int variable will contain a whole number and nothing else; because of this C# is considered a type-safe language. Some other programming languages allow variable to be created without fixed data types which can lead to errors that are very hard to debug.

Saturday 29 March 2014

Global.asax

Global.asax contains event handlers that are related to your applications and user sessions. Global.asax also contains an event handler that runs whenever an error happens anywhere in your application. By using Global.asax you can intercept every error that happens on your site. There are other event handlers in Global.asax that can also be useful.

Application_Start:
Runs when your site is started by the web server.

Application_End:
Runs when your site is stopped by the web server.

Application_Error:
Whenever there is a server error on your site.

Session_Start:
Runs when a user arrives at your site and start a new session.

Session_End:
Runs when a user session ends.

The most likely use of the Session_Start and Session_End events would be to log details of users when they arrive at your site.

You can also add other event handlers to Global.asax. There are many other application events that Global.asax can handle. A useful example is the Application_AuthenticateRequest event which runs before any page is served to a user, some programmers use this to implement their own custom login systems when they have special requirements that go beyond the built-in security features.

Friday 28 March 2014

Stored Procedures

Store Procedures are similar to methods but are stored inside the database. Store Procedures contains SQL code that can manipulate data in ways that would be difficult using C#. In a team environment store procedures are usually written by the DBA.

Store Procedures can have arguments and return values just like any other method. Store Procedures can do everything LINQ can do and usually be faster and more efficient. Using Store Procedures for everything might seem ideal but having hundred's of store procedures in a database often becomes unmanageable and difficult to maintain. I recommend using LINQ for simple everyday queries and store procedures for anything more complicated or if you see performance issues.

Using Store Procedures centralizes your SQL code on the database server and makes it easier to maintain your code if the database changes.

Thursday 27 March 2014

Collections Vs. Arrays

Collections are a more advanced way of storing multiple values inside a single object. Unlike arrays which have a fixed size once they are created, you can add and remove items from collections.
For most purposes collections are more convenient to work with then arrays.

Monday 24 March 2014

How to Configure Default Routing


We are given the above topology and we have to implement the following:

  1. Create default route on ISP router to the RISE STAR Enterprise Company. 
  2. Apply command show ip route to verify your configuration. 
Default Routing is very similar to the Static Routing as the pattern of configuring the Default Routing is identical to the Static Routing.

Now to start, click the ISP router and go to the CLI tab and type the following:

ISP>en
ISP#conf t
ISP(config) #ip route 0.0.0.0 0.0.0.0 se0/0/0
ISP(config) #ex
ISP#

Now click LAB_A router, go to the CLI tab and type the following:

LAB_A>en
LAB_A#conf t
LAB_A(config) #ip route 0.0.0.0 0.0.0.0 se0/0/0
LAB_A(config) #ip route 0.0.0.0 0.0.0.0 se0/0/1
LAB_A(config) #ex
LAB_A#

Now click on LAB_B router and go to the CLI tab and type the following:

LAB_B>en
LAB_B#conf t
LAB_B(config) #ip route 0.0.0.0 0.0.0.0 se0/0/0
LAB_B(config) #ip route 0.0.0.0 0.0.0.0 fa0/0
LAB_B(config) #ex
LAB_B#

Finally click on LAB_C router and go to the CLI tab and type the following:

LAB_C>en
LAB_C#conf t
LAB_C(config) #ip route 0.0.0.0 0.0.0.0 fa0/0
LAB_C(config) #ex
LAB_C#

Note that the first address(0.0.0.0) is used for the default address and the second(0.0.0.0) is used for the default sub-net mask and fa0/0 is the next hop address.
The task 1 is completed here.

Now the second task is to verify the configuration. To do this simply go to every router one by one and type the command show ip route in the CLI tab as shown below:


As you can see in the highlighted area the default routing is configured. If this line does not appear, it means you have not configured the routers correctly.

Sunday 23 March 2014

LINQ

Stands for Language Integrated Query. LINQ is a relatively new addition to .NET which makes working with databases much easier.

Before LINQ, programmers would usually create their own set of classes to generate the SQL needed to interact with the database, this took a lot of time and effort. ASP.NET's LINQ classes automatically create the SQL needed to interact with your data making it unnecessary to learn SQL. LINQ also streamlines the development process by removing the need to create your own data access classes.

It is important to note that although it is very easy to create LINQ data classes, they do not automatically update when the structure of a database changes; by structure I mean the design of the database not the data that is held inside it. The only way to update LINQ data classes is to recreate the entire dbml file, you can do this by simply deleting the old file and creating a new one. In theory it is possible to refresh individual items but I have found that this occasionally causes problems that prevent the project from building. For this reason I recommend that you always recreate the entire dbml file if your database structure changes.

Before LINQ, databases were usually accessed using the System.Data.SqlClient namespace of the .NET Library. System.Data.SqlClient is still available but it requires a lot more code to use. This method also requires you to either use Store Procedures or SQL code. LINQ has made System.Data.SqlClient obsolete but you may still see it being used in older projects.

Saturday 22 March 2014

DES Key Generation

Download the DES Key Generator to generate the Key with the help of Sub-Key.
Click the Link below to download the DES Key Generator.

DES Key Generator

DES Sub-Key Generation

Download the DES Sub-Key Generator to generate the Sub-Key 1 and Sub-Key 2 with the help of Key.
Click the Link below to download the DES Sub-Key Generator.

Thursday 13 March 2014

S-DES Encryption

Download the S-DES Encryption tool to encrypt the plain text with the help of Key.
If you do not have the key don't worry, it can also generate Key, Sub-Key 1 and Sub-Key 2.
If you have Sub-Key 2 you can find Sub-Key 1 and Key and if you have the Key, you can find Sub-Key 1 and Sub-Key 2 using the following tool.
Click the Link below to download the S-DES Encryption Tool.

S-DES ENCRYPTION TOOL

Wednesday 12 March 2014

Constructor in C#

A constructor is a method that runs whenever an instance of a class is created.

public MyClass()
{

}

This is a simple constructor, any code you put inside it will run when an instance of the class is created. In order to be recognized as a constructor the method must have the same name as the class it belongs to.

Notice how the syntax differs from a normal method, there is no void or other return value type specified before the method name. This syntax is peculiar to constructors.

public MyClass()
{
IntProperty = 15;// This will set the IntProperty property to 15 whenever a new instance of the class is created.
}

Just like any other method,constructors can have arguments.

public MyClass(int StartingNumber)//This adds an argument to the constructor called StartingNumber
{
IntProperty = StartingNumber;//This sets the IntProperty property to the number provided in the argument.
}

This should look very similar to you, it is exactly the same as adding arguments to any other method.

protected void Page_Load(object sender, EventArgs e)
{
MyClass MyClassInstance = new MyClass(15);//This provides the number 15 as the constructor argument. 
}

This means that when the constructor runs it will set the value of IntProperty to 15.

Sunday 9 March 2014

C# Dispose Method

Many classes have a method called Dispose(). The Dispose method is used to clear the class from memory. Some classes do not have dispose methods, this is usually because they don't have any properties that stay in memory. A link data class like the Form1DataContext class can load a lot of data into memory when it is used so it is good practice to dispose of it as soon as you no longer need it.

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
Form1DataContext Data = new Form1DataContext();//This creates a new instance of the Form1DataContext class called Data. 
Data.SubmitChanges();// This method is just an example of a method that you might call from the Data instance.
Data.Dispose();//This will remove the Data instance from memory.
}
Note that If you try to work with the Data instance after Data.Dispose(); line you would cause an exception.

As well as disposing of instances using the Dispose method you can use a using statement to automatically dispose of an instance.
Note that working with using statements inside your code is different to the using statements at the top of your page.

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
using(Form1DataContext Data = new Form1DataContext())//This creates a new instance of the Form1DataContext class called Data. 
{
Data.SubmitChanges();// This method is just an example of a method that you might call from the Data instance.
}
}

This does exactly the same thing as calling the Dispose method but is much easier to understand and work with. The using statement creates the Data instance and then automatically disposes of it after the last curly bracket.
Note that C# has a garbage collector that automatically disposes of objects that are no longer in scope. In this case for example the C# garbage collector would always dispose of the data instance after the ButtonSubmit_Click event handler was finished since it wouldn't be usable anymore. Despite this it is always best to dispose of instances where possible since the garbage collector doesn't always clean up memory immediately.

Thursday 6 March 2014

C# StringBuilder

The StringBuilder object is the more advanced version of the string variable that is designed to work better with very large strings. It is in the System.Text namespace so it is not easily available by default.

It is an auxiliary object used for manipulating characters. It contains a buffer, typically initialized with a string but usually larger than that string. This buffer can be manipulated in place without creating a new string: You can insert, append, remove, and replace characters. When you're done manipulating the characters, use StringBuilder's ToString method to extract the finished string from it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringBuilderClass
{
    class Program
    {
        static void Main(string[] args)
        {
            String TestStringBuilder;
            StringBuilder OtherStringBuilder = new StringBuilder("Hello");
            OtherStringBuilder.Remove(2, 3); // produces "He"
            OtherStringBuilder.Insert(2, "lp"); // produces "Help"
            OtherStringBuilder.Replace('l', 'a'); // produces "Heap"
            TestStringBuilder = OtherStringBuilder.ToString();
        }
    }
}

Wednesday 5 March 2014

Displaying Data from Multiple Tables


  • Write a query to display the last name, department number, and department name for all employees.
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;
  • Create a unique listing of all jobs that are in department 30. Include the location of department 90 in the output.
SELECT DISTINCT job_id, location_id
FROM employees, departments
WHERE employees.department_id = departments.department_id
AND employees.department_id = 80;
  • Write a query to display the employee last name, department name, location ID, and city of all employees who earn a commission.
SELECT e.last_name, d.department_name, d.location_id, l.city
FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND e.commission_pct IS NOT NULL;
  • Display the employee last name and department name for all employees who have an a (lowercase) in their last names.
SELECT last_name, department_name
FROM employees, departments
WHERE employees.department_id = departments.department_id
AND last_name LIKE '%a%';
  • Write a query to display the last name, job, department number, and department name for all employees who work in Toronto.
SELECT e.last_name, e.job_id, e.department_id, d.department_name
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
JOIN locations l
ON (d.location_id = l.location_id)
WHERE LOWER(l.city) = 'toronto';
  • Display the employee last name and employee number along with their manager’s last name and manager number. Label the columns Employee, Emp#, Manager, and Mgr#, respectively. 
SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager", m.employee_id "Mgr#"
FROM employees w join employees m
ON (w.manager_id = m.employee_id);
  • Modify above query to display all employees including King, who has no manager.
SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager", m.employee_id "Mgr#"
FROM employees w
LEFT OUTER JOIN employees m
ON (w.manager_id = m.employee_id);
  • Create a query that displays employee last names,department numbers, and all the employees who work in the same department as a given employee. Give each column an appropriate label. 
SELECT e.department_id department, e.last_name employee, c.last_name colleague
FROM employees e JOIN employees c
ON (e.department_id = c.department_id)
WHERE e.employee_id <> c.employee_id
ORDER BY e.department_id, e.last_name, c.last_name;
  • Show the structure of the JOB_GRADES table. Create a query that displays the name, job, department name, salary, and grade for all employees. 
DESC JOB_GRADES
SELECT e.last_name, e.job_id, d.department_name, e.salary, j.grade_level
FROM employees e, departments d, job_grades j
WHERE e.department_id = d.department_id
AND e.salary BETWEEN j.lowest_sal AND j.highest_sal;
  • Create a query to display the name and hire date of any employee hired after employee Davies.
SELECT e.last_name, e.hire_date
FROM employees e, employees davies
WHERE davies.last_name = 'Davies'
AND davies.hire_date < e.hire_date;
  • Display the names and hire dates for all employees who were hired before their managers, along with their manager’s names and hire dates. Label the columns Employee, Emp Hired, Manager, and Mgr Hired, respectively.
SELECT w.last_name, w.hire_date, m.last_name, m.hire_date
FROM employees w, employees m
WHERE w.manager_id = m.employee_id
AND w.hire_date < m.hire_date;

Saturday 1 March 2014

Single-Row Functions

  • Write a query to display the current date. Label the column Date.
SELECT sysdate "DATE" 
FROM dual;


  • For each employee display the employee ID number, last name, salary and salary increased by 15% and expressed as a whole number. Label the column New Salary.
SELECT employee_id, last_name, salary, ROUND(salary * 1.15, 0) "New Salary" 
FROM employees;


  • Modify above query to add a column that subtracts the old salary from the new salary. Label the column Increase.
SELECT employee_id, last_name, salary, ROUND(salary * 1.15, 0) "New Salary", ROUND(salary * 1.15, 0) - salary "Increase" 
FROM employees;


  • Write a query that displays the employee's last name with the first letter capitalized and all other letters lower case and the length of the names, for all employees whose name starts with J, A or M. Give each column an appropriate label. Sort the results by employee's last names.
SELECT INITCAP(last_name) "Name", LENGTH(last_name) "Length" 
FROM employees 
WHERE last_name LIKE 'J%' 
OR last_name LIKE 'A%' 
OR last_name LIKE 'M%' 
ORDER BY last_name;


  • For each employee display the employee's last name, and calculate the number of months between today and the date the employee was hired. Label the column MONTHS_WORKED. Order your results by the number of months employed. Round the number of months up to the closest whole number.
SELECT last_name, ROUND(MOTHS_BETWEEN(sysdate, hire_date)) "MONTHS_WORKED" 
FROM employees 
ORDER BY MONTHS_BETWEEN(sysdate, hire_date);


  •  Write a query that produces the following for each employee:
    <employee last name> earns <salary> monthly but wants <3 times salary>.
     Label the column Dream Salaries.
    Dream Salaries
    King earns $24,000.00 monthly but wants $72.000.00.
    Kochhar earns $17,000.00 monthly but wants $51.000.00.
    De Haan earns $17,000.00 monthly but wants $51.000.00.
    Hunold earns $9,000.00 monthly but wants $27.000.00.
    Emst earns $6,000.00 monthly but wants $18.000.00.
    Lorentz earns $4,200.00 monthly but wants $12.600.00.
    Morgous earns $5,800.00 monthly but wants $17.400.00.
SELECT last_name ||' earns '|| TO_CHAR(salary, 'fm$99,999.00') ||' monthly but wants '|| TO_CHAR(salary * 3, 'fm$99,999.00') ||'.' 
FROM employees;


  •  Create a query to display the last name and salary for all employees. Format the salary to be 15.
      
    LAST_NAME
    SALARY
    King
    $$$$$$$$$$24000
    Kochhar
    $$$$$$$$$$17000
    De Haan
    $$$$$$$$$$17000
    Hunold
    $$$$$$$$$$$9000
    Emst
    $$$$$$$$$$$6000
    Lorentz
    $$$$$$$$$$$4200
    Morgous
    $$$$$$$$$$$5800
SELECT last_name, LPAD(salary, 15, '$') "SALARY" 
FROM employees;


  • Display each employee's last name, hire date and salary review date, which is the first Monday after six months of service. Label the column Review. Format the dates to appear similar to "Monday, the Thirty-First of July, 2000."
SELECT last_name, hire_date, TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6), 'MONDAY'), 'fmDay, "the" Ddspth "of" Month, YYYY') "REVIEW" 
FROM employees; 


  • Display the last name, hire date and day of the week on which the employee started. Label the column DAY. Order the results by the day of the week starting with Monday.
SELECT last_name, hire_date, TO_CHAR(hire_date, 'DAY') "DAY" 
FROM employees 
ORDER BY TO_CHAR(hire_date -1, 'd');


  • Create a query that displays the employees last names and commission amounts. If an employee does not earn commission, put "No Commission." Label the column COMM. 
SELECT last_name, NVL(TO_CHAR(commission_pct), 'No Commission') "COMM" 
FROM employeees;


  •  Create a query that displays the employees last names and indicates the amounts of their annual salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
SELECT last_name ||' '|| RPAD(' ', salary/1000+1, '*') "EMPLOYEES_AND_THEIR_SALARIES"
FROM employees 
ORDER BY salary DESC;


  • Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID,as per the following data.
     
    Job
    Grade
    AD_PRES
    A
    ST_MAN
    B
    IT_PROG
    C
    SA_REP
    D
    ST_CLERK
    E
    None of the above
    0
 SELECT job_id, decode (job_id, 
                                             'ST_CLERK',       'E', 
                                             'SA_REP',           'D', 
                                             'IT_PROG',          'C', 
                                             'ST_MAN',           'B', 
                                             'AD_PRES',         'A', 
                                                                        '0') "G"     
FROM employees;


  • Rewrite the statement in the preceding question using the CASE syntax
SELECT job_id, CASE job_id 
               WHEN   'ST_CLERK'     THEN 'E' 
               WHEN   'SA_REP'         THEN 'D' 
               WHEN   'IT_PROG'       THEN 'C' 
               WHEN   'ST_MAN'        THEN 'B' 
               WHEN   'AD_PRES'      THEN 'A' 
               ELSE      '0'    END "G" 
FROM employees;