Data Bind To GridView and apply Pagging using DATATABLE to LINQ

Here I am Explaining Databind to GridView and apply Pagging for GridView using DataTale to LINQ.
1.Create new website and add page to your website in VISUAL STUDIO.
2.Design the page like this

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grid" runat="server" AllowPaging="true" PageSize="10" OnPageIndexChanging="grid_PageIndexChanging">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
3.Add the below code to your .cs file of the webpage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetProjectPool();
        }
    }
    public void GetProjectPool()
    {
        SqlConnection con = new SqlConnection("server=ADMINISTRATOR00;database=ProjectsHub;User Id=eoi_prasanna;password=987654");
        SqlDataAdapter adapter = new SqlDataAdapter("select *from ProjectsPool", con);
        DataTable table = new DataTable();
        DataTable table1 = new DataTable();
        adapter.Fill(table);
        var query = from row in table.AsEnumerable() select row;
        table1 = query.CopyToDataTable();
        DataView view = table1.DefaultView;
        grid.DataSource = view;
        grid.DataBind();
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        GetProjectPool();
    }
}
4.Then run your application or press F5.
the screen appears like below





then press on 2nd page then the 2nd page shows like below

enjoy Happy coding...takecare.

1 comments :

JavaScript Code To Disable Browser Back Button In Asp.net

Here I am explaining how to disable browser back button using javascript in Asp.Net
write the below code in head section of which page you want to disable back button..

<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">

        function DisableBackButton() {
            window.history.forward()
        }
        DisableBackButton();
        window.onload = DisableBackButton;
        window.onpageshow = function (evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function () { void (0) }
</script>
</head>
then run the page..

0 comments :

Allow Only Numbers In ASP TextBox and Html TextBox Using JAvaScript

Here I am Explaining asp TextBox and Html TextBox Allow only Digits Using JavaScript Code.
1. Design the aspx page like below.

<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    Asp TextBox:
                </td>
                <td>
                    <asp:TextBox ID="txtasp" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    HTML TextBox:
                </td>
                <td>
                    <input type="text" runat="server" id="txthtml" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
2.Add javascript code to aspx source page head section.
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">

    // JAVASCRIPT CODE FOR HTML TEXT BOX..
        function numeric(e) {
            var unicode = e.charCode ? e.charCode : e.keyCode;
            if (unicode == 8 || unicode == 9 || (unicode >= 48 && unicode <= 57)) {
                return true;
            }
            else {
                alert('Please Enter Digits Only')
                return false;
            }
        }

        // JAVASCRIPT CODE FOR ASP TEXT BOX..
        function isNumberKey(event) {
            var charCode = (event.which) ? event.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                alert("Pls Enter Digits Only");
                return false;
            }
            return true;
        }
    </script>
</head>
3.Apply the java script to textboxes like below..
<asp:TextBox ID="txtasp" runat="server" OnKeyPress="return isNumberKey(event);"></asp:TextBox>
<input type="text" runat="server" id="txthtml" onkeypress="return numeric(event); " />
4.then run the application or press F5 and observe the screen..




0 comments :

Delete Multiple Rows in GridView using ChekBox in ASP.NET

In this example i am explaining how to delete multiple rows records with Chekbox using C#.Net in Asp.Net
1. Design the aspx page like below

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td colspan="2">
    <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <input name="chek" type="checkbox" value='<%#Eval("Emp_Id")%>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Employee Id" DataField="Emp_Id" />
                <asp:BoundField HeaderText="Employee Name" DataField="Emp_Name" />
                <asp:BoundField HeaderText="Salary" DataField="Emp_Salary" />
            </Columns>
            <SelectedRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:GridView><br />
        <asp:Button ID="btndelete" runat="server" Text="Delete"
            onclick="btndelete_Click" />
            <br />
            <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>
    </td>
    </tr>
    </table>
       
    </div>
    </form>
</body>
</html>
The Page shows Like Below

2.Add the below code in .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    public void BindData()
    {
        SqlConnection con = new SqlConnection("server=ADMINISTRATOR00;database=Acadamics;uid=ACMCPRJ;pwd=projects");
        SqlDataAdapter da = new SqlDataAdapter("select * from Employee1", con);
        SqlCommandBuilder cmb = new SqlCommandBuilder(da);
        DataTable table = new DataTable();
        da.Fill(table);
        grid.DataSource = table;
        grid.DataBind();
    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        string empid = Request.Form["chek"];
        SqlConnection con = new SqlConnection("server=ADMINISTRATOR00;database=Acadamics;uid=ACMCPRJ;pwd=projects");
        SqlCommand cmd = new SqlCommand("Delete Employee1 where Emp_Id in (" + empid + ")", con);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
        string msg = "";
        if (i != 0)
        {
            msg = "Records deleted successfully..";
        }
        else
        {
            msg = "Records deletion failed..";
        }
        lblmsg.Text = msg.ToString();
        BindData();
    }
}
3.Then run the application or press F5.
The page shows like below
Select deleted records using chekbox and click on delete button. then the output will show below..









































0 comments :

Pagging and Sorting For GridView

Introduction:

Here I am explaining Paging and Sorting in GridView using Asp.Net and C#.Net.

Description:

I have one gridview with some data here my requirement is apply paging for the grid view and when i am click on column name the records in the table are sorted.
Now we will design the page like this
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Allow Paging and Sorting::Venu</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grid" runat="server" AllowPaging="true" AllowSorting="true" PageSize="5"
            OnPageIndexChanging="grid_PageIndexChanging" OnSorting="grid_Sorting" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField HeaderText="Employee Name" DataField="Employee" SortExpression="Employee" />
                <asp:BoundField HeaderText="Salary" DataField="Salary" SortExpression="Salary" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 After Completion of aspx page design write the following code in .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["Sortby"] = null;
        if (!IsPostBack)
        {
            BindData();
        }

    }
    public void BindData()
    {
        SqlConnection con = new SqlConnection("server=ADMINISTRATOR00;database=Acadamics;uid=ACMCPRJ;pwd=projects");
        SqlDataAdapter adapter = new SqlDataAdapter("select * from Employee", con);
        DataTable table = new DataTable();
        adapter.Fill(table);
        if (table.Rows.Count != 0)
        {
            DataView dv = table.DefaultView;
            if (Session["Sortby"] != null)
            {
                dv.Sort = Session["Sortby"].ToString();
            }
            grid.DataSource = table;
            grid.DataBind();
        }
       
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        BindData();
    }
    protected void grid_Sorting(object sender, GridViewSortEventArgs e)
    {
        Session["Sortby"] = e.SortExpression;
        BindData();
    }
}

Then run the application the page shows like below..

Employee Name Salary
venu 12000
surendra 10000
venu 10000
surendra 12000
gopi 1234
1 2
Click on column name Employee then sorting done and the page shows like below..

Employee Name Salary
gopi 1234
prasanna 4253245
surendra 10000
surendra 12000
venu 12000
1 2




0 comments :

Introduction To Object Oriented Programming Concepts (OOPS) In C#.Net


C# OOPS CONCEPTS:
CLASS:  Class is used to create user defined data types, Class contains variables and methods, variables are used to store values and methods are used to manipulate the values(accepting values from user, doing calculations, displaying result etc) It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created.
Creating a Class:
Class Employee //Creating Class..
{
Public int eno; //Declaring Variables..
Public string ename;
Public void GetEmpDetails() //Creating method..
{
//some code….
}
}
OBJECT: Object is instance of a class
An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. 
Object Creation:
Classname obj=new Classname();
Here obj => Creating variable/instance of class.
         New => Operator.
        Classname() => Constructor.

Encapsulation: Encapsulation is a process of Binding Variables and methods as a single unit. If a class is created then it is treated as an Encapsulation.
Inheritance:
Inheritance is a process of deriving a new class from already existing class. It is used for reusing the existing code.
Syntax:
Class ChildClassName : BaseClassName
{
       //Body..
}
Types of Inheritance:
1. Single Inheritance: When a single derived class is created from a single base class is called single inheritance.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace singleinheritanceexp
{
class Program
{
public void Display()
{
Console.WriteLine("Display method from base class");
}
class ChildAbc : Program //ChildAbc is child of Program
{
public void Display1()
{
Console.WriteLine("Display1 method from child class");
}
}
static void Main(string[] args)
{
ChildAbc objChildAbc = new ChildAbc(); //Creating object of child class
 objChildAbc.Display1();
 objChildAbc.Display(); //calling base class method
 Console.Read();
  }
  }
}
OutPut:
Display1 method from child class
Display method from base class

2. Hierarchical Inheritance: When more than one derived classes are created from a single base class is called Hierarchical Inheritance.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HierarchicalInheritanceexp
{
    class Program //Base Class..
    {
        public void Display()
        {
            Console.WriteLine("Display Method from base class");
        }
        class ChildAbc : Program //ChildAbc is child class of Program..
        {
            public void Display1()
            {
                Console.WriteLine("Display1 method from childabc class");
            }
        }
        class ChildDef : Program //ChildDef is child class of Program..
        {
            public void Display2()
            {
                Console.WriteLine("Display2 method from childdef class");
            }
        }
        static void Main(string[] args)
        {
            ChildAbc objChildAbc = new ChildAbc(); //Creating Object for childclass ChildAbc..
            objChildAbc.Display1();
            objChildAbc.Display(); // Calling base class method..

            ChildDef objChildDef = new ChildDef(); //Creating Object for childclass ChildDef..
            objChildDef.Display2();
            objChildDef.Display(); //calling base class method..

            Console.Read();
        }
    }
}
OutPut:
Display1 method from childabc class
Display Method from base class
Display2 method from childdef class
Display Method from base class

3.Multilevel Inheritance: When a derived class is created from a derived class is called as Multilevel Inheritance.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace multilevelinheritanceexp
{
    class Program //base class..
    {
        public void Display()
        {
            Console.WriteLine("Display method from base class");
        }
        class ChildAbc : Program //child class
        {
            public void Display1()
            {
                Console.WriteLine("Display1 method from first childclass");
            }
        }
        class SubChildAbc : ChildAbc //derived from child class ChildAbc
        {
            public void Display2()
            {
                Console.WriteLine("Display2 Method from subchild class");
            }
        }
        static void Main(string[] args)
        {
            SubChildAbc objSubChildAbc = new SubChildAbc(); //creating object for sub child class
            objSubChildAbc.Display2();
            objSubChildAbc.Display1();
            objSubChildAbc.Display();

            Console.Read();
        }
    }
}

OutPut:
Display2 Method from subchild class
Display1 method from first childclass
Display method from base class

4.Hybrid Inheritance: Combination of Single, Hierarchical and Multilevel Inheritances is called Hybrid Inheritance.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hybridinheritanceexp
{
    class Program
    {
        public void Display()
        {
            Console.WriteLine("Display method from base class");
        }
        class ChildAbc : Program
        {
            public void Display1()
            {
                Console.WriteLine("Display1 method from child class");
            }
        }
        class SubChildDef : ChildAbc //derived from child class ChildAbc..
        {
            public void Display2()
            {
                Console.WriteLine("Display2 method from subchild class1");
            }
        }
        class SubChildGhi : ChildAbc //derived from child class ChildAbc..
        {
            public void Display3()
            {
                Console.WriteLine("Display3 method from subchild class2");
            }
        }
        static void Main(string[] args)
        {
            SubChildDef objSubChildDef = new SubChildDef(); //cerating object for 1st subchildclass..
            objSubChildDef.Display2();
            objSubChildDef.Display1();
            objSubChildDef.Display();

            SubChildGhi objSubChildGhi = new SubChildGhi(); //cerating object for 2nd subchildclass..
            objSubChildGhi.Display3();
            objSubChildGhi.Display1();
            objSubChildGhi.Display();

            Console.Read();
        }
    }
}
OutPut:
Display2 method from subchild class1
Display1 method from child class
Display method from base class
Display3 method from subchild class2
Display1 method from child class
Display method from base class

5.Multiple Inheritance: When a derived class is created from multiple base classes is called Multiple Inheritance. But Multiple Inheritance is not supported by .net classes and can be done using Interfaces.

Polymorphism:
One name multiple forms are called Polymorphism. These are two types
1. Compile Time Polymorphism or Early Binding.
2. Run Time Polymorphism or Late Binding.
1.Compile Time Polymorphism: Compile Time Polymorphism Is Method and Operator Overloading.
Method Overloading:
Multiple methods having same name and different signature is called method overloading. In this Compiler will take decision at compile time which method will execute based on passed arguments.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace methodoverloadingexp
{
    class Program
    {
        public int Sum(int a, int b)
        {
            return a + b;
        }
        public int Sum(int a, int b, int c)
        {
            return a + b + c;
        }
        class Abc : Program
        {
            public int Sum(int a, int b, int c, int d)
            {
                return a + b + c + d;
            }
        }
        static void Main(string[] args)
        {
            Abc objAbc = new Abc();
            Console.WriteLine(objAbc.Sum(25,50,13,24));
            Console.WriteLine(objAbc.Sum(25, 50, 13));
            Console.WriteLine(objAbc.Sum(25, 50));
            Console.Read();
        }
    }
}
OutPut:
112
88
75


2.Run Time Polymorphism: Run Time Polymorphism Is done by using Inheritance and Virtual functions.
Method Overriding:
It is possible only with inheritance, the methods in different classes having same signature then methods in one class will overrides the method in another class that is derived class method will override the base class method, same signature means method names number of arguments , order of arguments, data types will be same, method overriding is called runtime polymorphism. Based on the method address compiler will take decision at run time that which method in the class should execute.  
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace methodoverridingexp
{
class Program
{
public virtual void Manipulation(int a, int b) //creating virtual method..
{
Console.WriteLine(a + b);
}
class Abc : Program
{
public override void Manipulation(int a, int b) //creating override method..
{
base.Manipulation(20, 30); //calling base class method..
Console.WriteLine(a - b);
}
}
static void Main(string[] args)
{
Abc objAbc = new Abc(); //creating object for derived class..
objAbc.Manipulation(30, 20); //calling override method..
Console.Read();
}
}
}
OutPut:
50
10
Abstraction:
Abstraction is a process of hiding the implementation but providing the services.
Abstraction is possible with inheritance and Abstract contains abstract and non abstract methods if it contains at least one abstract method then only it is treated as abstract class. And an Abstraction method contains only method declaration, method implementation in the derived class.
How to Abstract: By using Access Specifiers.
In C# Five Access Specifies are there these are 
1. Public: Accessible outside the class through object reference.
2. Private: Accessible inside the class only through member functions. Even in main function also it is not accessible.
3. Protected: It is just like private but Accessible in derived classes also through member functions.
4. Internal: Accessible with in the Assembly through objects.
5. Protected Internal: It is the combination of Protected and internal, Visible inside the assembly and in derived classes outside the assembly through member functions.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace abstractexp
{
   abstract class Program
    {
       //Abstract Method..
       public abstract void Display();

       //Non Abstract Method..
       public void Display1()
       {
           Console.WriteLine("Non Abstract Display1 Method");
       }

       class Abc : Program
       {
           public override void Display() //implementing the Abstract method
           {
               Console.WriteLine("Abstract Display Method in Abc Class");
           }
       }
       class Def : Program
       {
           public override void Display() //implementing the Abstract method
           {
               Console.WriteLine("Abstract Display Method in Def Class");
           }
       }
         static void Main(string[] args)
        {
            Abc objAbc = new Abc(); //creating object for 1st derived class Abc..
            objAbc.Display1();
            objAbc.Display(); //calling abstarct method..

            Def objDef = new Def(); //creating object for 2nd derived class Def..
            objDef.Display1();
            objDef.Display(); //calling abstract method..

            Console.Read();
        }
    }
}

OutPut:
Non Abstract Display1 Method
Abstract Display Method in Abc Class
Non Abstract Display1 Method
Abstract Display Method in Def Class

Interface : Interface is like a class but with interface we can’t create object, Interface can contains only Abstract members, Interface members can be Events, Methods, Properties and Indexers and Interface can contains only declaration for its members and implementation in the derived class and Interface members are public abstract by default, With Interfaces Multiple Inheritance is possible.

Simple Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace simpleinterfaceexp
{
    class Program
    {
        interface IAbc
        {
            void Sum(int a, int b);
        }
        class Abcd : IAbc
        {
            void IAbc.Sum(int a, int b)
            {
                Console.WriteLine(a + b);
            }
        }

        static void Main(string[] args)
        {
            IAbc obj; //Declaration..
            obj = new Abcd();//Creating Object..
            obj.Sum(20, 30);
            Console.Read();
        }
    }
}
OutPut:
50

Example: Two Interfaces in one class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace twointerfacesoneclas
{
    class Program
    {
        interface IClient1 //First Interface..
        {
            void GetEmpDetails(int eno, string ename);
        }
        interface IClient2 //Second Interface..
        {
            void GetEmpDetails(int dno, string dname);
        }
        class Client : IClient1, IClient2 //Using Two Interfaces in One class..
        {
            void IClient1.GetEmpDetails(int eno, string ename)
            {
                Console.WriteLine(eno + " " + ename);
            }
            void IClient2.GetEmpDetails(int dno, string dname)
            {
                Console.WriteLine(dno + " " + dname);
            }
        }
        static void Main(string[] args)
        {
            IClient1 objIClient1 = new Client();
            objIClient1.GetEmpDetails(1, "Venu");

            IClient2 objIClient2 = new Client();
            objIClient2.GetEmpDetails(10, "Developer");

            Console.Read();
        }
    }
}
OutPut:
1 Venu
10 Developer

Example: One Interface In another Interface..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oneinterfaceinanotherinterface
{
    class Program
    {
        interface IClient1
        {
            void GetEmpDetails(int eno, string ename);
        }
        interface IClient2:IClient1
        {
            void GetDeptDetails(int dno, string dname);
        }
        class Client : IClient2
        {
            void IClient1.GetEmpDetails(int eno, string ename)
            {
                Console.WriteLine(eno + " " + ename);
            }
            void IClient2.GetDeptDetails(int dno, string dname)
            {
                Console.WriteLine(dno + " " + dname);
            }
        }
        static void Main(string[] args)
        {
            IClient2 objIClient2 = new Client();
            objIClient2.GetEmpDetails(1, "Venu");
            objIClient2.GetDeptDetails(10, "Development");
            Console.Read();
        }
    }
}
OutPut:
1 Venu
10 Development

Example: One Interface In Two classes..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oneinterfaceintwoclasses
{
    class Program
    {
        interface IManipulation //Creating Interface..
        {
            void Display(int a, int b);
        }
        class MyClass1 : IManipulation //Using Interface in First Class..
        {
            void IManipulation.Display(int a, int b)
            {
                Console.WriteLine("Sum Is:" + (a + b));
            }
        }
        class MyClass2 : IManipulation //Using Interface In second class..
        {
            void IManipulation.Display(int a, int b)
            {
                Console.WriteLine("Sub Is:" + (a - b));
            }
        }
        static void Main(string[] args)
        {
            IManipulation objIManipulation; //Declaration..

            objIManipulation = new MyClass1(); //creating object for 1st class..
            objIManipulation.Display(30, 20);

            objIManipulation = new MyClass2(); //creating object for second class..
            objIManipulation.Display(30,20);

            Console.Read();
        }
    }
}
OutPut:
Sum Is:50
Sub Is:10


0 comments :