Tuesday, 15 November 2016

Exception and Exception handling (Part-1)

Que. What is an Exception?
Ans. Generally, We are saying exception is runtime error. But it is           not exactly exception is runtime errors. First of all try to                  understand what is an exception. Whenever we are developing        an application, then becomes two types of errors.

       1. Compile time error
       2. Runtime error.
  
Compile time Error
--------------------------
   The error that occurs in the program due to syntactical mistake is called compile time error. Syntactical mistake means forget the end statement through ;(semi colon), Curly braces ({}) are not in proper, Strings are not in double quotes etc... .These all things are called syntactical mistake and these mistakes under the compile time errors.
Compile time errors are not dangerous because compiler recognized the syntax error and give the list of mistake that's why compile time error is not dangerous.

Runtime error
-------------------
    The error that occurs in the code due to various reasons. These errors becomes not at the time of compilation of the code. These errors come at the time of execution of our application. Means execute the code and error will come is called runtime error.
Now, Why we get runtime error?
 There are various reasons to get runtime errors.

   => Wrong implementation of logic

         Ex:- Suppose 
         
                  int[] Objarr= {15,25,10,40,35}
                  for(int i=0;i<=Objarr.Length;i++)
                    {
                        Console.WriteLine(i);
                     }
         
         Here above the example we can see array declare withe length of five and in the for loop  logic execute in six times. So, here wrong implementation of logic is used.

   => Wrong input Supplied
         Ex:- We make an application and in the application have the input field string and numeric both and any one put in string field numeric value and in the numeric field string value at that time wrong input supplied.

   => Missing required resource. etc...
        Ex:- Suppose we have an application to read a resource like web cam, printer, projector etc... and when we start the application and at that time these resources are not detected by the application in this situation we got a runtime error Missing required resources.

Note:- Runtime errors are very dangerous because it is abnormal termination of the program. Whenever an error occurs inside the code the statement terminate and rest of the code is not execute that's why runtime error is very dangerous. 

Abnormal termination:

------------------------------
  Abnormal termination means when execute the code and error come inside the code, then application terminate that particular line of code without executing the rest of the line.

Ex:- Suppose we have a program for division:
        
        using System;
         class Demo
         {
           public static void Mai()
            {
               Console.Write("Enter 1st Number");
               int x=int.parse(Console.ReadLine());               
               Console.WriteLine("Enter 2nd Number");
               int y=int.parse(Console.ReadLine());
               int z=x/y;
               Console.WriteLine("Result :"+z);
               Console.ReadLine();
            }
         }

In the above program suppose if error becomes in the line of int x=int.parse.......; then program abnormally terminated without executing of rest of the code and give the runtime error. This is called abnormally termination.  

=> Now, In your mind hit one question, Who is the responsible for abnormal termination when ever runtime error occurs inside the code ?
So, the answer is Exception is a class which is responsible for abnormal terminating the program whenever errors occurs inside the program.
Whenever errors occurring in the program, then immediately exception classes comes into the picture for abnormal termination of the program.
There are so many exception classes are present, which is display different-different messages in different scenario. These are as follows:- 
  - IndexOutOfBoundException
  - DivideByZeroException
  - OverflowException
  - FormatException   etc....

These are the classes responsible for abnormally terminated the program whenever errors occur in the program.

Que: Why so many Exception classes are their?
Ans: Because in different-different scenario error message will                  become in different ways and these exception classes describe          about the error message in which scenario errors is occurs.               There are representing each and every error one class has been         defined. 
Here above mention exceptions are pre-defined exception classes.
Predefined classes means these are available in our library.

Read More »

Sunday, 6 November 2016

LINQ (Language Integrated Query) Part-2

LINQ (Language Integrated Query) Part-2

Microsoft in 3.5 framework introduce LINQ (Language Integrated Query). It is given as a query language which we can write a query on wide variety of data sources. like we can write queries on Relational databases, XML files, arrays, Collections.
Before LINQ we have SQL (Structure Query Language) and we can write the query for Relational Database. but in the LINQ we can write the query for wide variety of data sources.

So,LINQ is basically you can divide in different categorisation. These are as follows:

 =>   LINQ to Objects 
 =>   LINQ to Database
 =>   LINQ to XML

LINQ to Objects
   Using this category of LINQ we can write the query on array, collection etc.

LINQ to Databases
   Using this category of LINQ we can write the query on Data tables, Relational Database tables i.e. 
      - LINQ to ADO.Net
      - LINQ to SQL
      - LINQ to Entities 

LINQ to XML
  Using this category of LINQ we can write the query on XML files.

Now i am going to discuss about LINQ to SQL.

Linq to Sql
  
     It is a query language which is introduce in .Net 3.5 framework for working with relational database i.e. Sql server only for Sql server relational database not for other relational databases.
Linq to Sql is not only about querying the data but also us to perform CRUD operation i.e. Insert, Update, Delete operation.
CRUD means :- [Create (insert), Read (Select),Update, Delete].

Note :- We can also call the Stored Procedure by using Linq to Sql.

And last one Linq to Entities : Using this Linq to Entities we can write the query on Sql server relational databases as well as Other relational databases like Oracle, etc...



Now Let's People raise the Question here :

=>We have already a query language which is called SQL server and using ADO.Net we interact with database. Then why LINQ again. ?

Ok Now try to understand differences between Sql interact with Sql Server and Linq interact with Sql Server.

Now, differences between Sql interact with Sql Server and Linq interact with Sql Server these are as follows:-

      Sql to Sql Sqrver                               Linq to Sql Server

1. Run time syntax checking of    1. Compile time syntax checking.

    Sql Server statement.
2. Not type safe.                            2. Type safe.
3. No intelligence support.            3. Intelligence support.   
4. Debugging of Sql statement      4. Debugging of Linq to Sql is 
    is not possible.                             available.
5. Code is combination of             5. It is pure Object Oriented.
    Object Oriented and relational
    database. 


Runtime syntax checking of sql server statement:
    Run-time syntax checking of sql server statement means we write the sql query under the double quotes in c#.net/vb.net then c# or vb compiler treat that query as a string. that's why C# or vb compiler do not compile that query at compile time. Because C# or VB compiler never compile the string.
When we write the query under the application within double quotes then every time the ADO.net carry the query and send to the database. After that Database Engine verify the syntax is define correct or not and the statement is defined perfectly or not. All the verification is done by the database engine not by the C# or vb compiler.
The main drawback is suppose if you are run the application in hundred machine at a time then every time and every machine send the statement to database and database engine verify and compile that statement so at that time extra burden on the server.

Compile time syntax checking:
  Compile time syntax checking means Linq queries is not verify by database engine. Because Linq queries is purely defined in our .Net languages. If you are working in C#.net then you write the Linq query in C# code and if you are working in VB.net then you write the Linq query in VB code. So,Internally their is a Linq query engine under our framework and this is a part of our framework.
And all the statement what ever you write in native language  compile by C# or VB compiler itself. C# or VB compiler only verifies weather the syntax is correctly define or not and also verify that statement is perfectly return or not.
The main advantage is statement is not compile each and every time because our application is compile one and only one time so, our statement is also compile one time and at the point of compile time verify the syntax and statement is perfectly defined or not. So, if you run your application in hundred machine then it is not required compile your program each and every time.

No type safe
  When we work with sql server with the help of ADO.net then the responsibility of developer remember everything about table that is a column name, data type and its size. Because here intelligence is not come that why the developer has remembered everything.
Suppose a developer develop an application and don't verify about data that means which type of data send in particular column, then at that time all the data goes to the database and after that database engine verify that data. Their after exception send to the application ,in this process only wastage of time. So, when working with sql server, developer responsible to verify everything about data.

Type safe
  When we work with Linq to Sql then no need to remember about tables column name and its data type because visual studio intelligence is appear particular table column name.

Debugging of Sql statement is not available
  In the application when we use SQL server query, then every statement write within the double quotes that's why our c#/vb compiler don't understand SQL statement and treat that statement as string. That's why in SQL statement in the application debugging is not possible. 

Debugging of Linq to Sql is available
 In the application when we use Linq to SQL then no need to write Linq query in double quotes. That's why our c#/vb compiler understand that statement and debugging in Linq to SQL is possible.

Code is combination of object Oriented and Relational.
 Code is a combination of Object-Oriented and relational such as suppose when we writing the insert/select/update/delete statement using sql under ado.net to interact with database means in the application we write the sql query inside the double quotes because our c#/vb compiler don't understand sql. So you responsible for pass the value which is came from either text box/checkbox/radiobutton/drop down etc... outside the double quotes. 
Ex:-  "insert into table_name values("+textbox1.text+", '"+textbox2.text+"')";

here in this example when we pass the textbox1 value in as a numeric value and textbox2 pass the string value that's why pass textbox2 value in single quotes.
Through this example we can see and understand code is combination of object oriented and relational under the sql to sql server. And it is work on the table, row, column, store-procedures etc...
 
It is pure Object Oriented.
  Linq to sql is pure object oriented because here no any relational code and no need to write the query in double quotes. suppose you write the code select statement in Linq to sql then write the codes directly because these codes are c#/vb statement only and these codes are compiled by the c#/vb compiler. The most important things is here when we are working with Linq to Sql we don't have tables at all, every tables become a class.

      Sql to Sql Server                               Linq to Sql
     ----------------------                         --------------------
        Tables                        =>                 Class
         - Columns                 =>                   -Property
         -Rows or records      =>                   -Instance
         - Stored procedures  =>                   - Methods
 In the Linq to sql every tables become to class. every columns goes to property, every row or records goes to Instance and every stored procedures goes to methods.


Read More »