Monday, October 21, 2019

Learn About OOPS Concept and How to make Classes

Learn About OOPS Concept and How to make Classes

In this article we will learn about OOPS and Classes


# OOPS

Object oriented Programming Structure
Class/business object
-         Collection of similar type of data

#Project Modeling

* 2-Tier Architecture

- Database tier= tables, stored procedure,primary key, foreign key etc
- Presentation Tier = Where D.T is accessed , design, create connection and others in code file.

* 3-Tier Architecture

-D.T
- Middle Tier/ Business Logic Layer/ Business Tier
In MT- whole business logic is written and DT is accessed
-PT
BT is accessed in PT

* N-Tier Architecture

   SER 1(Data will come from it)                SER 2 (Data will come from it)

#-Classed used will be non- compiled classes
-Non- Complied classes are compiled with project
- Their Scope is within the application
- Cannot be used more than 1 project

#Open Visual Studio and make New Project 

*Create Connection String
<connectionStrings>
    
    <add name="psg" connectionString="server=DESKTOP-I3R20OL\SQLEXPRESS; database=database name; integrated security=true"/>
  </connectionStrings>

*Add new item>class

App_Code(folder)- All non-complied classes are stored in it

*Remove all the data below name spaces

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace nsemployees
{
    public interface intemp
    {
        Int32 empno { get; set; }
        String ename { get; set; }
        String eadd { get; set; }
        Int32 esal { get; set; }
    }
}



#Interface- It is used for declaration (member declare). It is not used for definition.
We can inherit the interface within the interface. We can implement multiple interfaces in a single class. We can use interface for standardization (standard code).
Column name and Property name is same.

In property, Only 2 blocks are used ‘get’ and ‘set’.
Get- to read data from property
Set- to write data from property

# How to create properties

    public class clsempprp : intemp
    {
        private Int32 prvempno, prvesal;
        private String prvename, prveadd;
        public int empno
        {
            get
            {
                return prvempno;
            }
            set
            {
                prvempno = value;
            }
        }
        public string ename
        {
            get
            {
                return prvename;
            }
            set
            {
                prvename = value;
            }
        }
        public string eadd
        {
            get
            {
                return prveadd;
            }
            set
            {
                prveadd = value;
            }
        }
        public int esal
        {
            get
            {
                return prvesal;
            }
            set
            {
                prvesal = value;
            }
        }
    }

public class clsempprp : intemp – to implement interface
*Click on dropdown> click on implement interface
*Number of properties= number of variables
In public int empno, we learn how to create properties and value is keyword
 # Make abstract class
public abstract class clscon
    {
        protected SqlConnection con = new SqlConnection();
        public clscon()
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
        }
    }

public abstract class clscon
 

connection class and it will be 1 in whole project

*Abstract Class- It can be inherited only but object can never be made. It can be used to pass the reference. Only 1 class can be inherit but it can be multi level but not multiple.
*Protected – scope upto drive class only means can only be used where it is inherited.
public clscon() – it is constructor

# Save code

public class clsemp : clscon
    {
        public void Save_Rec(clsempprp p)
        {
            if (con.State==ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("insemp", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@eno", p.empno);
            cmd.Parameters.AddWithValue("@en", p.ename);
            cmd.Parameters.AddWithValue("@ed", p.eadd);
            cmd.Parameters.AddWithValue("@es", p.esal);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }

public class clsemp : clscon
 

    derived class       base class
cmd.ExecuteNonQuery(); - to execute stored procedure

# Update code

public void Update_Rec(clsempprp p)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("updemp", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@eno", p.empno);
            cmd.Parameters.AddWithValue("@en", p.ename);
            cmd.Parameters.AddWithValue("@ed", p.eadd);
            cmd.Parameters.AddWithValue("@es", p.esal);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }

 # Delete Code

public void Delete_Rec (clsempprp p)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("delemp", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@eno", p.empno);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }

#Display Code

public List<clsempprp>Disp_Rec()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("dispemp", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();
            List<clsempprp> obj = new List<clsempprp>();
            while (dr.Read())
            {
                clsempprp k = new clsempprp();
                k.empno = Convert.ToInt32(dr[0]);
                k.ename = dr[1].ToString();
                k.eadd = dr[2].ToString();
                k.esal = Convert.ToInt32(dr[3]);
                obj.Add(k);
            }
            dr.Close();
            cmd.Dispose();
            con.Close();
            return obj;
        }

List<>- it is collection
Collection- Array of object

#Find Code

public List<clsempprp>Find_Rec(Int32 eno)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("findpemp", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@eno", eno);
            SqlDataReader dr = cmd.ExecuteReader();
            List<clsempprp> obj = new List<clsempprp>();
            if (dr.HasRows)
            {
                clsempprp k = new clsempprp();
                k.empno = Convert.ToInt32(dr[0]);
                k.ename = dr[1].ToString();
                k.eadd = dr[2].ToString();
                k.esal = Convert.ToInt32(dr[3]);
                obj.Add(k);
            }
            dr.Close();
            cmd.Dispose();
            con.Close();
            return obj;
        }

# Design code


* Object Data Source > Task List> Configure data source
* Select Business Object > Nsemployees:clsemp>Next> Disp
* Listbox >Task List >Choose Datasource> Object Data Source > ename > empno >enable Autopost

# Code file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    nsemployees.clsempprp objprp = new nsemployees.clsempprp();
    nsemployees.clsemp obj = new nsemployees.clsemp();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        objprp.empno = Convert.ToInt32(TextBox1.Text);
        objprp.ename = TextBox2.Text;
        objprp.eadd = TextBox3.Text;
        objprp.esal = Convert.ToInt32(TextBox4.Text);
        obj.Save_Rec(objprp);
        ListBox1.DataBind();
        Clear_Rec();
    }

    private void Clear_Rec()
    {
        TextBox1.Text = String.Empty;
        TextBox2.Text = String.Empty;
        TextBox3.Text = String.Empty;
        TextBox4.Text = String.Empty;
        TextBox1.Focus();
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<nsemployees.clsempprp> k;
        k = obj.Find_Rec(Convert.ToInt32(ListBox1.SelectedValue));
        TextBox1.Text = k[0].empno.ToString();
        TextBox2.Text = k[0].ename;
        TextBox3.Text = k[0].eadd;
        TextBox4.Text = k[0].esal.ToString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        objprp.empno = Convert.ToInt32(TextBox1.Text);
        objprp.ename = TextBox2.Text;
        objprp.eadd = TextBox3.Text;
        objprp.esal = Convert.ToInt32(TextBox2.Text);
        obj.Update_Rec(objprp);
        ListBox1.DataBind();
        Clear_Rec();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        objprp.empno = Convert.ToInt32(TextBox1.Text);
        obj.Update_Rec(objprp);
        ListBox1.DataBind();
        Clear_Rec();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Clear_Rec();
    }
}

# Object Data Source = It is always used with 3-Tier architecture. It is used with business object. It is stateless(When it will use the object , It will automatically destroy it)
# DLL – Dynamic Link Library (Complied Classes)
-         We can use in multiple application
-         It is used in more than 1 project.
-         These are In-Process component (Run on same process where application running).
-         It is part of COM(Component Object Model)
-         It is self described.
-         COM inter-portability  support

# COM- It is an software architecture which is used to design software component.

No comments:

Post a Comment