Friday, 23 December 2016

Delegate

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

namespace OopsConcept
{
    class Delegate
    {
    }
    public delegate int NumberChanger(int w);
    namespace DelegateAppl
    {
        class TestDelegate
        {
            static int num = 10;
            public static int AddNum(int p)
            {
                num += p;
                return num;
            }

            public static int MultNum(int q)
            {
                num *= q;
                return num;
            }
            public static int getNum()
            {
                return num;
            }

            static void MainDelegate(string[] args)
            {
                //create delegate instances
                NumberChanger nc1 = new NumberChanger(AddNum);
                NumberChanger nc2 = new NumberChanger(MultNum);

                nc1(25);
                nc2(35);
                Console.WriteLine("Addition of two number is {0}",getNum());
                Console.ReadLine();
            }
        }
    }
}

Constructor

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

namespace OopsConcept
{
    class Constructor1
    {
        public Constructor1()
        {
            Console.WriteLine("constructr that have the no parameter");

        }
        //public Constructor1(string st)
        //{
        //    Console.WriteLine("constructr that have the  parameter {0}",st);

        //}
        public Constructor1(string st1, string st2)
        {
            Console.WriteLine("constructr that have the  parameter {0}&{1}", st1,st2);

        }
    
    }
     class ChildClass : Constructor1
    {
        public ChildClass()
            : base()
        {
            Console.WriteLine("Chuild  Class constructor with n parameter ");
        }
        //public ChildClass(string stchild1)
        //    : base(stchild1)
        //{
        //    Console.WriteLine("Child class cnstructr that have one string parameter{0}", stchild1);

        //}
        public ChildClass(string stchild1,string stChild2)
            : base(stchild1,stChild2)
        {
            Console.WriteLine("Child class cnstructr that have one string parameter{0} & {1}", stchild1, stChild2);

        }
    }
    public class Program1
    {
        public static void Maincons(string[] args)
        {
            Console.WriteLine("--------------------------Cnstructor Chaining strted here---------------------------------------");
            ChildClass objConstructor = new ChildClass ();
            ChildClass objCh2 = new ChildClass("Parameter1", "Parameter2");
            Console.ReadLine();
        }
    }
}

ABSTTRACTION

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

namespace OopsConcept
{
    public class Bike
    {
        public int mileage = 65;
        public string color = "Black";
        private string formula = "a*b";
        //Its public – so accessible outside class
        public int GetMileage()
        {
            return mileage;
        }

        //Its public – so accessible outside class
        public string GetColor()
        {
            return color;
        }

        //Its private – so not accessible outside class
        private string GetEngineMakeFormula()
        {
            return formula;
        }
    }
    public class hondaBike
    {
        public int mileage = 65;
        public string color = "Black";
        private string formula = "a*b";
        //Its public – so accessible outside class
        public int GetMileage()
        {
            return mileage;
        }

        //Its public – so accessible outside class
        public string GetColor()
        {
            return color;
        }

        //Its private – so not accessible outside class
        private string GetEngineMakeFormula()
        {
            return formula;
        }

        //Its public – so accessible outside class
        public string DisplayMakeFormula()
        {
            //"GetEngineMakeFormula()" is private but accessible and limited to this class only
            return GetEngineMakeFormula();
        }
    }

    public class Programabst
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("------------------------------------Encapsulation started here------------------------------------");
            Bike objBike = new Bike();
            Console.WriteLine("Bike mileage is : " + objBike.GetMileage()); //accessible outside "Bike"
            Console.WriteLine("Bike color is : " + objBike.GetColor()); //accessible outside "Bike"
            //we can't call this method as it is inaccessible outside "Bike"
            //objBike.GetEngineMakeFormula(); //commented because we can't access it


            Console.WriteLine("------------------------------------Encapsulation End here------------------------------------");



            Console.WriteLine("------------------------------------Abstraction Started here------------------------------------");
            hondaBike objhondaBike = new hondaBike();
            Console.WriteLine("Bike mileage is : " + objhondaBike.GetMileage()); //accessible outside "Bike"
            Console.WriteLine("Bike color is : " + objhondaBike.GetColor()); //accessible outside "Bike"
               //we can't call this method as it is inaccessible outside "Bike"
               //objBike.GetEngineMakeFormula(); //commented because we can't access it
            Console.WriteLine("Bike making frmula  is : " + objhondaBike.DisplayMakeFormula()); //accessible outside



            Console.WriteLine("------------------------------------Abstraction End here------------------------------------");
            Console.Read();
        }
    }
}

AbstractInterfaceBoth

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

namespace OopsConcept
{
    /// <summary>
    /// An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
//Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
//We can not declare a member field in an Interface.
//We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
//An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
    /*
     * Abstraction:
Abstraction is "To represent the essential feature without representing the back ground details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or object by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in understandable manner.

Real world Example of Abstraction: -
Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as following:-

Nokia 1400 (Features:- Calling, SMS)
Nokia 2700 (Features:- Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:-Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (Necessary and Common Information) for the object "Mobile Phone" is make a call to any number and can send SMS."

so that, for mobile phone object you will have abstract class like following:-

     *
     ///  ENCAPSULATION START HERE
     *
     *
     *
     * Encapsulation:
Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

    class Bag
    {
        book;
        pen;
        ReadBook();
    }


Encapsulation means hiding the internal details of an object, i.e. how an object does something.

Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from the other object.

Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.
So, when you access the property you can validate the data and set it.
S
    
    
    
     */
    /// </summary>
abstract class M1
   {
        public int add(int a, int b)
        {
            return (a + b);
        }
    }
    class M2 :M1
    {
        //public int mul(int a, int b)
        //{
        //    return a * b;
        //}
    }
    class test
    {
        static void Main56(string[] args)
        {
            M2 ob = new M2();
            int result = ob.add(10, 20);
            Console.WriteLine("the result is {0}", result);
            Console.ReadLine();
        }
    }
}


Sunday, 21 February 2016

nested gridediting in asp.net

code//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class NestedGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvDepartments.DataSource = GetData("select top 10 * from Departments");
            gvDepartments.DataBind();
        }
    }

    private static DataTable GetData(string query)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string DepartmentId = gvDepartments.DataKeys[e.Row.RowIndex].Value.ToString();
            GridView gvEmployees = e.Row.FindControl("gvEmployees") as GridView;
            gvEmployees.DataSource = GetData(string.Format("select top 3 Id,Name,Address,DepartmentId from Employees where DepartmentId='{0}'", DepartmentId));
            gvEmployees.DataBind();
        }
    }

    protected void gvEmployees_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gvEmployees = sender as GridView;
        GridViewRow row = gvEmployees.Rows[e.NewEditIndex];
        int DepartmentId = Convert.ToInt32((row.Cells[0].FindControl("lblDepartmentId") as Label).Text);
        gvEmployees.EditIndex = e.NewEditIndex;

        gvEmployees.DataSource = GetData(string.Format("select top 3 Id,Name,Address,DepartmentId from Employees where DepartmentId='{0}'", DepartmentId));
        gvEmployees.DataBind();

    }

    protected void gvEmployees_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView gvEmployees = sender as GridView;
        int Id = Convert.ToInt32(gvEmployees.DataKeys[e.RowIndex].Value);
        this.Delete(Id);
        gvEmployees.EditIndex = -1;
        Response.Redirect("CS.aspx");
    }

    private void Delete(int id)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string sqlStatment = "DELETE FROM  Employees WHERE Id = @id";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }

    protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
    {
        GridView gvEmployees = sender as GridView;
        int id = Convert.ToInt32(((Label)gvEmployees.Rows[e.RowIndex].FindControl("lblId")).Text);
        string name = ((TextBox)gvEmployees.Rows[e.RowIndex].FindControl("txtName")).Text;
        string address = ((TextBox)gvEmployees.Rows[e.RowIndex].FindControl("txtAddress")).Text;
        int index = name.IndexOf(",");
        string finalName = name.Substring(index + 1);
        index = address.IndexOf(",");
        string finalAddress = address.Substring(index + 1);
        index = address.IndexOf(",");
        this.Update(id, finalName, finalAddress);
        gvEmployees.EditIndex = -1;
        gvDepartments.DataSource = GetData("select top 10 * from Departments");
        gvDepartments.DataBind();
    }

    private void Update(int id, string name, string address)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string sqlStatment = "UPDATE Employees SET Name = @Name,Address = @Address WHERE Id = @id";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Parameters.AddWithValue("@Name", name);
                cmd.Parameters.AddWithValue("@Address", address);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}




//design



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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .Grid td
        {
            background-color: #A1DCF2;
            color: black;
            font-size: 10pt;
            line-height: 200%;
        }
        .Grid th
        {
            background-color: #3AC0F2;
            color: White;
            font-size: 10pt;
            line-height: 200%;
        }
        .ChildGrid td
        {
            background-color: #eee !important;
            color: black;
            font-size: 10pt;
            line-height: 200%;
        }
        .ChildGrid th
        {
            background-color: #6C6C6C !important;
            color: White;
            font-size: 10pt;
            line-height: 200%;
        }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("[src*=plus]").live("click", function () {
            $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
            $(this).attr("src", "images/minus.png");
        });
        $("[src*=minus]").live("click", function () {
            $(this).attr("src", "images/plus.png");
            $(this).closest("tr").next().remove();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="gvDepartments" runat="server" AutoGenerateColumns="false" CssClass="Grid"
                DataKeyNames="Dept_Id" OnRowDataBound="OnRowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <img alt="" style="cursor: pointer" src="images/plus.png" />
                            <asp:Panel ID="pnlEmployees" runat="server" Style="display: none">
                                <asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"
                                    OnRowDeleting="gvEmployees_OnRowDeleting" OnRowUpdating="UpdateCustomer" OnRowEditing="gvEmployees_OnRowEditing"
                                    CssClass="ChildGrid">
                                    <Columns>
                                        <asp:TemplateField HeaderText="DepartmentId" Visible="false">
                                            <ItemTemplate>
                                                <asp:Label ID="lblDepartmentId" Text='<%# Eval("DepartmentId") %>' runat="server"></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Id">
                                            <ItemTemplate>
                                                <asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server"></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField ItemStyle-Width="100px" HeaderText="Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name")%>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField ItemStyle-Width="100px" HeaderText="Address">
                                            <ItemTemplate>
                                                <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address")%>'></asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("Address")%>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:ButtonField CommandName="Edit" Text="Edit" />
                                        <asp:ButtonField CommandName="Delete" Text="Delete" />
                                        <asp:ButtonField CommandName="Update" Text="Update" />
                                    </Columns>
                                </asp:GridView>
                            </asp:Panel>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField ItemStyle-Width="150px" DataField="Dept_Id" HeaderText="Dept Id" />
                    <asp:BoundField ItemStyle-Width="150px" DataField="Dept_Name" HeaderText="Dept Name" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>



//dadatabse



create database tempdb1
CREATE TABLE [dbo].[Departments](
    [Dept_Id] [int] IDENTITY(1,1) NOT NULL,
    [Dept_Name] [varchar](50) NOT NULL,
 CONSTRAINT [PK_Departments] PRIMARY KEY CLUSTERED
(
    [Dept_Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO



CREATE TABLE [dbo].[Employees](
    [Id] [int] NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [Address] [varchar](100) NOT NULL,
    [DepartmentId] [int] NULL,
 CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

nested grid edit

http://www.aspforums.net/Threads/133072/Edit-Update-Delete-in-Nested-Child-GridView-in-ASPNet/

Friday, 19 February 2016

insert data with check exist or not

   if not exists (select AssessmentName from AssessmentDetails where ClientID=43  and ProgramName='Accenture (BPO)' and CourseName='Effective Business Writing' and AssessmentName='test' )
                        BEGIN
                            insert into AssessmentDetails (ClientId,ProgramName,CourseName,AssessmentName,MaximumScore,CreatedBy,Status) values (
                              43,'Accenture (BPO)','Effective Business Writing','test','688',578,'True')
                              select 1 as p
                                end
                              else
                                BEGIN
                                select 0 as p
                                end