Sunday, 14 May 2017

Program

coming soon....... 
Read More »

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 »

Sunday, 23 October 2016

LINQ ( Language Integrated Query) Part-1

LINQ ( Language Integrated Query)

In C#, Programming with  objects means a wonderful strongly typed ability to work with code. You can navigate very much easily through the namespaces, work with a debugger in Visual Studio IDE,and more however, when you have to access data, you will notice that things are dramatically different, i.e. not Strongly typed. And you end up spending most of the time sending strings to the database as commands.

That's why Microsoft developed a new query language name as LINQ (Language Integrated Query) which is developed in .Net 3.5 Framework. It is a new query language very much similar to SQL.
Microsoft has provide LINQ as a lightweight facade that provides a strongly typed interface to the underlying data stores.
Using the LINQ we can write queries on a wide variety of data sources like Array, Collections, Database tables, Data set, XML data. LINQ provides the means for developers to stay within the coding environment they are used to and access the underlying data as Objects that work with IDE,Intelligence and even debugging with LINQ.

Example:-
Before LINQ suppose we have an array of data and we want to find the data through some condition then write so many lines of code after that we get the appropriate data from array. But when LINQ is introduce the it is easy to write the code for finding the data. Let's all these things are show in the example below:-

Ex:- Suppose we have an array and we want to find the data from the array which are greater than 49.


using System;
using System.Linq;
using System.Text;

namespace LINQProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = {33,44,85,94,67,15,28,37,46,97,61,73,13,27};
            int count = 0;
            for(int i=0;i<arr.Length;i++)
            {
                if(arr[i]>49)
                {
                    count += 1;
                }
            }
            int[] brr = new int[count];
            int index = 0;
            for (int i = 0; i < arr.Length;i++ )
            {
                if(arr[i]>49)
                {
                    brr[index] = arr[i];
                    index += 1;
                }
            }
            foreach(int x in brr)
            {
                Console.Write(x + " ");
            }
            Console.ReadLine();
        }
    }
}

Output:-


here above the solution without using LINQ. Now i am writing the code using LINQ.

using System;
using System.Linq;
using System.Text;

namespace LINQProject
{
    class linq1
    {
        public static void Main(string[] ares)
        {
            int[] arr = { 33, 44, 85, 94, 67, 15, 28, 37, 46, 97, 61, 73, 13, 27 };
            var brr = from i in arr where i > 49 orderby i select i;
            foreach(int x in brr)
            {
                Console.Write(x + " ");
            }
            Console.ReadKey();
        }
    }
}

Output:-


Now you can see the difference the code when we use with LINQ or without LINQ.
Now you can read Linq part 2 Click Here>>>



Read More »

Monday, 27 June 2016

Basics Of C#.net

Advantage of .NET
 -      -> Platform independence
     ->  Security
     ->  Automatic memory management

    Development of .net framework
      The development of .net framework started the Microsoft in late 90’s under the name NGWS(Next Generation Windows services/System).To develop the framework, first a set of specification have been prepared known as CLI(Common language infrastructure) specification. CLI specification are open specification standardized under ISO & ECMA(Europium computer Manufacture Association) that give a chance for develop the framework by anyone. The CLI specification talks about these important things:-
       ->  CLS(Common Language Specification)
          ->  CTS(Common Type System)
       ->  BCL(Base Class Library)
       ->  VES(Virtual Execution System)
       ->  CLR(Common Language Run-time)

CLS (Common Language Specification)
            CLS is set of basic rules of all the .Net Language. It has to adopt to inter-operate with each other, Most important after compilation of any .net language program they should give the same output code i.e. CIL code.
As for example:-

  C#.net ->  CSC  ->  CIL Code {C Sharp Compiler (CSC)}
 VB.net ->  VBC ->  CIL Code {Visual basic Compiler (VBC)}
 J#.net   ->  JSC  ->  CIL Code  {J sharp Compiler (JSC)}
 F#.net  ->  FSC  ->  CIL Code  {F Sharp Compiler (FSC)} 

CTS (Common Type System)
               According to this all languages of .Net has to adopt uniform data structure i.e. similar data types must be same in size under all language of .net, so that if any two language wants to communicate with each other size problem with data type will not come.

NOTE:
----------
CLS & CTS are foundation for language inter-operability of .net languages.

Que. Data types are mismatched. How to inter-operability work?
Ans. Before compilation re-usability is not possible. It is only                 possible after compilation. Because after compilation there            will be no error. Any data types that is used in any .Net language       once after compilation get converted into IL types as following:-

          C#.Net Data type      ->      Compiled   ->       CIL Code
         ----------------------                                        ------------------

                    Int                                                    int32
                    Float                                                single               
                    bool                                                  Boolean

          VB.Net Data type     ->      Compiled   ->    CIL Code
          ----------------------                                     ------------------

                    Integer                                             int32
                    single                                               single
                    boolean                                            boolean
So the data types name will be different from language to language, but even if names are different, similar data types will be uniform in size.

So, in IL (Intermediate language) all data type will be same. When we want to consume the code of a language from other .net language. The data type of 1st language are first converted to IL type and then presented to the 2nd language as its understandable data types as following:-




Library

-> Library is nothing but it is a set of re-usability functionality.
-> In the C/C++ library are called header files.
-> In java library are called Packages
-> And in .Net are called Base class library.

BCL (Base Class Library)
                  A library is a set of reusable functionalities where each and every programming language has built in library. Same as .Net languages are provided with built in libraries known as Base class libraries. The specialty of these library is that they can be consumed under any .net language.

                  



NOTE:-
-----------
BCL are best example of language inter-operability because these library are developed in c# language & can be consumed from any .net language.

.NET Framework Versions:-
       


RTM- Release to manufacturer 



.Net Framework Architecture:-

                       
                   fig: .Net Framework Architecture

      TPL - Task Parallel Library
         PLINQ - Parallel Language Integrated Query
      LINQ - Language Integrated Query
     WPF - Windows Presentation Foundation
     WCF - Windows Communication Foundation
     WF/WWF - Windows Workflow Foundation
     ADO - Active-x Data Objects
     ASP - Active Server page

CLR or VES
It is execution engine of .NET Framework, where all .Net applications are running under supervision of CLR & it provides various features (benefits) to applications like :-
       ->Security
       ->Platform Independent Management
       ->Automatic Memory management
          ->Runtime Error Handling


CLR internally contains the following things in it :-
        1. Security Manager
     2. Class loader
     3. JIT (Just In Time) Compiler
     4. Garbage Collector
     5. Exception Manager

=> Security Manager is responsible for taking care of the security of the application i.e. it will allow application to interact directly with the OS or OS to interact with application.








Read More »

Monday, 6 June 2016

CURSOR

      What is Cursor ? 
                  It is a memory allocation. i.e. used for storing multi row select statements data under the program. Normal variables can store only one value at a point of time where as a cursor can store n number of records at a point of time. 
To work with a cursor we need to understand cursor management which involves in 5 steps.

   Step 1 : Declaring a Cursor
   Step 2 : Opening a Cursor
   Step 3 : Fetching data from the Cursor
   Step 4 : De-allocation the Cursor
   Step 5 : Closing a Cursor 
  
  Declaring a Cursor 
              In this process we declare a cursor and intimate to the database engine that we want a memory  allocation for processing the select statement associate with the cursor declaration. 
 Syntax:-
                Declare Cursor_name cursor
                [ Local | Global ]
                [ Forward only | Scroll ]  
                [ Static | Dynamic ]
                for <Select Statement>

  Opening a Cursor 
                In this process it will allocate the memory for the cursor and loads that a into the cursor by execution the select statement  specified on the declare cursor.
  Syntax:-  
               open <cursor_name> 
Note:
    After loading the data into the cursor it will also provide a pointer for accessing the data from the cursor.

Fetching data from the cursor 
                In this process we retrieve one by one record from the cursor for processing.
  Syntax:-
                Fetch First | Last |Next | Prior | Absolute n | Relative n                       from cursor_name into  <variables>

When we perform  a fetch operation it will return status of the last fetch statement and value can be any of the following.

   0The fetch statement was successful.
 - 1 : The fetch statement failed or the row was beyond the result set
 - 2 : The row fetched is missing.

 Closing the cursor  
                In this process it releases that current result set or data from the memory but leaves the data structure  available for re-opening. 
   Syntax:- 
                Close <cursor_name>
   
De-Allocation the Cursor  
                    In this process it will remove the cursor references and De-allocates the data structure of the cursor.
   Syntax:- 
              Deallocate <cursor_name>; 

For Example applying all step :-
----------------------------------------
Suppose we have a table Employee
   Step 1 :   
        Declare @ename varchar(50), @job varchar(40);       
        Declare EmpCur Cursor for select Empname,job from employee
Step 2 :
        open empcur
Step 3:
        fetch next from empcur into @ename,@job
        while @@fetch_status=0
          Begin
                print @ename + ' ' +@ job;
                fetch next from empcur into @ename,@job;
          End
Step 4 :
       close empcur;
Step 5:
       deallocate empcur;
Note:-
     The value that returns by the fetch statement is stored under an implicit local variable  '@@fetch_status'

 [Local or Global ] cursor
---------------------------------
                                 If the cursor is declare as 'local' the scope of the cursor is only to the program in which the cursor is declare. So in the end of the program execution even if we did not De-allocate the cursor also the cursor gets De-allocate implicitly.
Ex:-       
        Declare @empno int
     Declare EmpCur Cursor local for select Empno from employee
        open empcur;
        fetch next from empcur into @empno;
        while @@fetch_status=0
          Begin
                print @empno;
                fetch next from empcur into @empno;
          End
      close empcur;
      deallocate empcur;
                                               Open a new query window and write the above code in it, we declare the above cursor as a local cursor. So even if we did not de-allocate the cursor also in the end of the program the cursor gets de-allocate. So to test this declare cursor statement and execute the program for second time get the error.


Note:-
---------
  If the cursor is declare as 'global' and not de-allocated that cursor can be used in the other program also with in the connection.
Ex:-       
 Declare @empno int
    Declare EmpCur Cursor global for select Empno from employee
        open empcur;
        fetch next from empcur into @empno;
        while @@fetch_status=0
          Begin
                print @empno;
                fetch next from empcur into @empno;
          End
      close empcur;
      deallocate empcur;
write the above program in a new connection window and execute, we declare the cursor as global in a program and did not de-allocate cursor that means  without re-deallocate.
To test this comment the declare cursor statement and execute the same program for n number of times the connection.
Ex:-

Declare @empno int
--declare Empcur cursor global for select empno from Employee
--open empcur
 fetch next from empcur into @empno
  while @@FETCH_STATUS=0
      begin
         print @empno
         fetch next from empcur into @empno
     end

close empcur

Note:-
-----------
All global cursors declared under a connection or de-allocation once we close the connection.

[ Static or dynamic ] cursor


-----------------------------------
                           If a cursor is declared as a static after opening the cursor and loading data into the cursor if any modification are made to the data in the table those changes will not be reflected in to the cursor. But 
If the cursor is declare as dynamic the modification of the table reflects into the cursor even after the cursor is open also.
Ex:-
    Declare @sal money
    declare Empcur cursor Static for select salary from Employee           where Empno=1001
      open empcur
       update employee set salary+=500 where Empno=1001
       fetch next from empcur into @sal
    print @sal
close empcur

  output:
-----------
In the above case we declare the cursor as static and open the cursor for loading data into the cursor. So, After opening the cursor the update operation we performed on the table will not reflect into the cursor. So, Print statement will print the old salary of the employee but not in updated value.
Execute the same program by changing static as dynamic and watch the output the print statement will display the new salary of the employee.
Ex:-
    Declare @sal money
    declare Empcur cursor dynamic for select salary from Employee           where Empno=1001
      open empcur
       update employee set salary+=500 where Empno=1001
       fetch next from empcur into @sal
    print @sal
close empcur
  output:
-----------

[ Forward only or scroll ] cursor


----------------------------------------
                                          In the cursor is declare forward-only we can navigate only to the next record from the current cursor position. The only fetch method support in forward-only cursor is fetch next.
Where as if a cursor is declared as scroll we can navigate to any record under the cursor from any position because scroll cursor support 6 fetch method.
  i) Fetch Next
 ii) Fetch prior
iii) Fetch  First
iv) Fetch  Last
 v) Fetch  Absolute
vi) Fetch relative n

Ex:-
     Declare @empno int
     declare Empcur cursor scroll for select empno from Employee
     open empcur
        fetch last from empcur into @empno
             print @empno
        fetch prior from empcur into @empno
             print @empno
        fetch first from empcur into @empno
             print @empno
        fetch next from empcur into @empno
             print @empno
        fetch absolute 3 from empcur into @empno
             print @empno
        fetch relative -3 from empcur into @empno
             print @empno
  close empcur

Output:-
-----------

Read More »