Monday, 22 January 2018

001 C# Introduction to Object Oriented Programming

C# tutorial 001 

Introduction to Object Oriented Programming

Technological world is dynamic . New tools and Technology coming day by day , and this
process is vast. Software Industry also come under the process , new tools and 
technologies are coming to meet common people requirement, to make life easier to easier and
and removing the manual job with machine made work .The fast changing world made software 
industry in a crisis . The most challenging work is to represent an entity which match with the 
real life world , to improve quality of software and to design a software which can communicate
with other software technology and and communicate cross platform Technology . During design a
software engineer consider some following basic points 

  • User friendliness 
  • Security and integrity
  • Correctness 
  • Usability 

Object oriented programming like C + + , C # treat data as an element who is cannot 
free flow to the program . Object oriented programming bind data functions together
 which is called class . A data can be accessed by only the function associate with it . 
No other object function class or system know the internal structure of the objects . This method
 is called data binding . When we try to modify the data or access the data ,it can call only
by the functions . When two objects communicate between each other , without knowing the 
internal structure of each other , they called only functions associate with the other object . 
This is called data hiding . The following points are important to understand the concept of oops

  • Objects 
  • Function 
  • Classes 
  • Inheritance 
  • Polymorphism 
  • Dynamic binding 
  • Message passing 
  • Data abstraction and encapsulation 
  • Variable initialization

 objects : objects  is an run time   basic entity  of an  object oriented system .  An object    
content data and function associate with the data . An object can be anything to represent
a real life world . Car , student ,   football , computer anything . Example of an object.

public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public Student(string name, int age)
{
Name = name;
Age = age;
}
//....other function and methods
}



functions : Function is an  system define programming which can access data of an object .
 It not only access the data , can communicate  with other object also. Functions and heart of  
OOPS concept . Function can have different accessibility modifier depending upon
 the requirement . It can be private , public , protected , friend . Depending upon the requirement 
to match of the real life world  , software engineer   define  the accessibility modifier of  a 
function . 

classes  : We already discussed that an object contain data and the functions ,the interstate 
of code which is the system we define is called class . Once a class is defined we
 can create any number of instance of an class .Thus a class is a collection of objects of similar
 types .

public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public Student(string name, int age)
{
Name = name;
Age = age;
}
//....other function and methods
}
 


Inheritance : Inheritance is a property which can acquire the property of other class . A class 
name x , another class name y , we can derive the property of x to y , bye inheritance . 
We just need to tell the class who it is its parents class . Entire property of x will be drive to 
y or automatically . 



Polymorphism : Polymorphism means more than one forms . And operation can be exhibit
 different behavior on different instance on different situation . For a simple example , if we call
 a function parameter with a single string object , it will call the function which has only string 
parameter . Maybe there function with different parameter with same name , function with 
same signature will be called and execute .

There are two types of polymorphism static or compile time polymorphism and dynamic 
or runtime polymorphism .In static static polymorphism the method which will be called 
as decided during the compile time.Oveloding is the best example of static polymorphism .
This type of polymorphism compiler check the Parameter type number during compile time .

Runtime polymorphism which is called method overriding method overriding is a technique to 
overide the base class to derived class.This type of polymorphism compiler does not know 
the type and the parameter during compile time , at runtime it will be decided ,which method
will be called .If may match then execute or through error.It is called dynamic binding .

class Student
{
    public class OverloadTest
    {

        public void Getdata(string a1, string a2)
        {
            Console.WriteLine("Adding Two String :" + (a1 + a2));
        }

        public void Getdata(int a1, int a2)
        {
            Console.WriteLine("Adding Two Integer :" +( a1 + a2));
        }

    }

    static void Main(string[] args)
    {
        OverloadTest obj = new OverloadTest();

        obj.Getdata("Hello ", "World");

        obj.Getdata(5, 10);

        Console.ReadLine();
    }
}



Message passing : object oriented programming is object base , it is the heart of the 
approach . Object to object communication is also necessary . Object can communicate with
 other object by passing passing message as the real human does. 

Benefit of object oriented programming as follows 
  • Complexity software development can be easily handled .
  • Future enhancement and development of father recruitment can be easily options .
  • We can build a small software to a lot software gradually without affecting the user regular work .
  • Data partition help us to keep data secure from other objects .
  • Message passing can communicate between objects to object which meets the real life world activity .
  • Redundant code of existing system can be eliminated.
  • Real time system can be developed with large data.

Tuesday, 1 November 2016

Asp.Net Catching

Catching is a technique to get frequently used data/ Information /pages instantly without re-processing web page from server.The object is stored in cache memory and deliver to browser when asked , this makes first operation & less load on server . To generate new HTML & data , is costly for server and it is also time consuming also , bit if stored in memory , retrieve data will be much more efficient than retrieve from database.That is the utility of catching .For example , we are developing an e commerce application , price list of item will not change daily , may be change once in a week.Now if we put the price list in catching , then e commerce site will work much faster.Generating a web page need several stage Page request, Page Initialization , Load , validation , rendering , unload new connection establishment with database , query execution , fetching data.But catching , just retrieve data from cache .Net framework in run time key-value map CLR object called cache .A catching is effective till
  • The catching lifetime , whcih has expired
  • The application has released memory
  • In any case ,catching fails
There are three type of catching in Asp.net Output catching , data catching , object catching. The scope of the data catching is within the application domain ,Catching comes under System.Web.Caching namespace .One point to remember , we can clear browser history to clear cache .Cache can be removed cache remove command

Cache.Remove("MyChache");


Output Catching : Output catching stored finally rendered HTML copy to cache memory.It is simple in implementation and very efficient in working .Output catching reduced response time and work still cache expires. @ OutputCache is the directive to declare output catching , several parmeter are there to maintain catching .For example

HTML
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ OutputCache Duration="100" VaryByParam="None" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
</body>
</html>

 C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.Sleep(1000);
        Response.Write("This page generated and cached at:"+System.DateTime.Now.ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

Runtime



 In the above example , page generation time is same , when we click on the button , the page is not re created in server , it is being server by catching after the very first time.

Data Caching : It is a technique to catch the data at the end . Data catching is effective as the previous condition satisfied .
  • The catching lifetime , which expires
  • The application released memory
  • In any case , catching does not occurred
Object Catching : Object catching is the catching a object from web page .Catching an object of an page is much more efficient than catch the entire page.Objects catching can catch complex object also .Still now we came to know , what is catching , type of catching and how catching works .Now we will go though parameter & configuration related to catching .First we will learn that .How we can control where catche can be stored .We can define it .The attribute is HttpCacheability.


HttpCacheability.Server :It tells that , the cache can be store any location /client /host/Proxy.

HttpCacheability.ServerAndNoCache :The response is catche at server & client and not any other location like proxy server.

HttpCacheability.Private :It tells that , the catche can be on the client only not on server or proxy server.

HttpCacheability.Public : Catching stored client & shared (proxy) server.
HttpCacheability.NoCache :Disable Catche

Sample Code :

    protected void Button1_Click(object sender, EventArgs e)
    {
         Response.Cache.SetExpires(System.DateTime.Now.AddDays(5);
         Response.Cache.VaryByHeaders["MyCache"]=true;
         Response.Cache.SetCacheability(HttpCacheability.Private);
    }

 Other than this , there are several ways to clear cache & control cache also . For example if we set 
<meta http-equiv="cache-control" content="no-catche" />

The catche will not work for this page , clear catching instantly for this page.
Previous , we learned about  @ OutputCache to set catche in HTML ,Now
@ OutputCache have some attribute


Thursday, 27 October 2016

Microsoft SQL Server Trigger

Microsoft SQL Server have opportunity "Trigger" ."Trigger" is not like stored procedure or SQL Query , which need to execute ."Trigger" is an functionality object which fires himself when data in a table modified (Insert/Update/Delete). The firing will be determined by defination of trigger , when trigger will fired on Insert/Update/delete .Actually trigger is special kind of stored procedure automatically execute on event occur .We can create/alter trigger by simple DML statement.Trigger are generally execute to implement some business rules. , for example when I am creating a student records , it is obvious to create a login for that student.Here ,if we write a trigger which will create a login data when a new student inserted .Trigger will automatically fires when we add a new student and no need ti write extra logic or not extra event is required to create student login.Her is an exaple of trigger


CREATE TRIGGER myTrigger ON tbl_member
FOR INSERT,UPDATE, DELETE
AS
SELECT MEMBER_ID,MEMBER_NAME FROM Inserted
SELECT MEMBER_ID,MEMBER_NAME FROM deleted
--Wirte your next code here

In teh above exaple , we have a table tbl_membe .We have creatde a trigger on tbl_member .We have define the trigger "FOR INSERT,UPDATE, DELETE" .That means , any operation INSERT,UPDATE, DELETE , the trigger will fired .Now , what happen when Insert, Update , Delete happen ? There is a concept of magic table .During Insert/Update/Delte two type of magic table are created .
  1. Insertde
  2. Deleted
  Inserted tahble for Inserted value 
  Deleted table for deleted value
  For updated , both inserted & deleted table works , first deleted then inserted.

Some points to remember
  • Sql Server allow more than one trigger on a table
  • When we have multiple trigger on a table , we can use ,"sp_settriggerorder" to set trigger firing order .For example trigger A will fire first , trigger B will fire next
                              An Example of Trigger

 
CREATE Trigger Mytrigger1 ON [dbo].[tbl_student]
FOR INSERT
AS
DECLARE @l_student_name         VARCHAR(500),
            @l_student_dob          DATETIME,
            @l_student_class      INT,
            @l_student_address      VARCHAR(MAX)

SELECT @l_student_name=student_name,
         @l_student_dob=student_dob,
         @l_student_class=student_class,
         @l_student_address=student_address
                                      FROM INSERTED;
--Insert Audio Table
INSERT INTO tbl_student_audion
(
      student_name,
      student_dob,
      student_class,
      student_address
)
VALUES
(
     @l_student_name,
       @l_student_dob,
       @l_student_class,
       @l_student_address
)  
GO

In the above exaple , a table named tbl_student hilds , students records , when we insert data into tbl_student , the same data will be copied into tbl_student_audit without any extra event for fireing.

How ever , trigger are basically two types 

  1. After Trigger
  2. Instead of trigger
After Trigger
This type of trigger fires automatically after an insert/update/delete .After insert/After update/After Delete .This type of trigger fires only after main operation (Insert data).In the above example we can modify thus

CREATE Trigger Mytrigger1 ON [dbo].[tbl_student]

AFTER INSERT
AS
DECLARE @l_student_name         VARCHAR(500),
            @l_student_dob          DATETIME,
            @l_student_class      INT,
            @l_student_address      VARCHAR(MAX)

SELECT @l_student_name=student_name,
         @l_student_dob=student_dob,
         @l_student_class=student_class,
         @l_student_address=student_address
                                      FROM INSERTED;
--Insert Audio Table
INSERT INTO tbl_student_audion
(
      student_name,
      student_dob,
      student_class,
      student_address
)
VALUES
(
     @l_student_name,
       @l_student_dob,
       @l_student_class,
       @l_student_address
)  
GO
                      In this process , first record are inserted in the tbl_student table and then data copied tbl_student_audit table. If we change it to "After Delete" , the first data will be deleted and the data will be inserted to tbl_student_audit table.After trigger are the only trigger to allow the same record that client application currently has locked(The operation that caused the trigger to fire earlier or first phase.


                     Instead of Insert 
Instead of Insert trigger fires  before the original operation Insert/Update/Delete .Actually it replace the original operation , if an instead of delete trigger exits on a table , when client delete record , the trigger code will be fired and control return to client.Now question is , when we will use Instead of Trigger ? When you modify the changes made by the client before actually posting update , Instead of Trigger used.

 
CREATE Trigger Mytrigger1 ON [dbo].[tbl_student]
ALTER DELETE
AS
DECLARE @l_student_name         VARCHAR(500),
            @l_student_dob          DATETIME,
            @l_student_class      INT,
            @l_student_address      VARCHAR(MAX)

SELECT @l_student_name=student_name,
         @l_student_dob=student_dob,
         @l_student_class=student_class,
         @l_student_address=student_address
                                      FROM DELETED;
--Insert Audio Table
INSERT INTO tbl_student_audion
(
      student_name,
      student_dob,
      student_class,
      student_address
)
VALUES
(
     @l_student_name,
       @l_student_dob,
       @l_student_class,
       @l_student_address
)  
GO

Now , if i have a multiple trigger on a table , how to disable or on/off trigger ?
The syntax of trigger enable /disable is

ALTER TABLE table_name DISABLE TRIGGER tr_name
ALTER TABLE table_name ENABLE TRIGGER tr_name 

For example  , i want to disable "Mytrigger1" trigger from "tbl_student"
The sql will be

ALTER TABLE tbl_student ENABLE TRIGGER Mytrigger1

A Practical Example
 
--I am creating two table test1 and test2
 CREATE TABLE tests1
 (
 id INT IDENTITY,
 sname VARCHAR(50),
 age INT
 )
 CREATE TABLE tests2
 (
 id INT IDENTITY,
 sname VARCHAR(50),
 age INT
 )
 --I am inserting value to tests1
 INSERT INTO tests1(sname,age)
 VALUES('Name1',12)
 GO
 INSERT INTO tests1(sname,age)
 VALUES('Name2',13)

 --Now table test1 has 2 records and test2 is empty


CREATE Trigger Mytrigger1 ON [dbo].[tests1]
AFTER INSERT
AS
DECLARE @l_sname  VARCHAR(50),
        @l_age    INT
       
--Initialised variable
SELECT @l_sname='',
       @l_age=NULL
      
--get value from test1 into variable
SELECT @l_sname=sname,
       @l_age=age
       FROM INSERTED;

--insert data to test2
 INSERT INTO tests2(sname,age)
 SELECT @l_sname,@l_age

GO
---Now i am again addtin data to tests1
 INSERT INTO tests1(sname,age)
 VALUES('Name2',13)

 ---trigger will fires and put data to tests2

SELECT * FROM tests2

 
        

                      




বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...