Share Blog

Tuesday, May 27, 2014

LINQ TO SQL :AND ,OR,less Then,Equal ,Greater Then,Less Then OPERATOR


--------------------------CREATE A TABLE---------------------------
create table Operator(Id int primary key, Name varchar(50),Country varchar(50),City varchar(50),Salary float)
select * from Operator
--------------INSERT DATA IN TABLE--------------------
insert into Operator values(1,'Mukesh', 'India','Sikar',5000),(2,'Mohit', 'SriLanka','colmbo',8000),
(3,'Naveen', 'Pakistan','Panjab',9000),(4,'Shivam', 'India','Jaipur',15000),(5,'Bichitra', 'India','Noida',10000

1. Create a Data Context Class

I create a data context class that has tables or a stored procedure. One figure shows the Operator table in a data context class and another all employee data in the database. This DATACLASS object has a relationship with the Operator table.







------Default.aspx.cs code--------

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
{
    DataClassesDataContext dc = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        Show();
    }
    void Show()
    {
        //Data maping object to our database
        var v = from i in dc.Operators select i;
        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var n = Convert.ToInt32(TextBox5.Text);
       
        // use for salary greater than  given salary
      
        var v = from i in dc.Operators where i.Salary > n select i;
      
        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();

       
        Page.RegisterStartupScript("open", "<script>alert('Greater Than Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
       

    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        var Name = TextBox2.Text;
        var City = TextBox4.Text;

        // update the salary of employee based on name and city
       
        var v = from i in dc.Operators where i.Name == Name && i.City == City select i;
        foreach (var item in v)
        {
            item.Salary = Convert.ToInt32(TextBox5.Text);
           
        }
        dc.SubmitChanges();
        Page.RegisterStartupScript("open", "<script>alert('AND Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        //Data maping object to our database
        var b = from i in dc.Operators select i;
        //Data bind And Show Data in GridView
        GridView1.DataSource = b;
        GridView1.DataBind();

    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        var Name = TextBox2.Text;
        var City = TextBox4.Text;

        // update the salary of employee based on name and city

        var v = from i in dc.Operators where i.Name == Name || i.City == City select i;
        foreach (var item in v)
        {
            item.Salary = Convert.ToInt32(TextBox5.Text);

        }
        dc.SubmitChanges();
        Page.RegisterStartupScript("open", "<script>alert('OR Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        //Data maping object to our database
        var b = from i in dc.Operators select i;
        //Data bind And Show Data in GridView
        GridView1.DataSource = b;
        GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        var n = Convert.ToInt32(TextBox5.Text);

        // use for salary Less than  given salary

        var v = from i in dc.Operators where i.Salary < n select i;

        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();


        Page.RegisterStartupScript("open", "<script>alert('Less Than Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        var n = Convert.ToInt32(TextBox5.Text);

        // use for salary Equal  given salary

        var v = from i in dc.Operators where i.Salary == n select i;

        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();


        Page.RegisterStartupScript("open", "<script>alert('Equal Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
       
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        var n = Convert.ToInt32(TextBox5.Text);

        // use for salary Greater Than and Equal  given salary

        var v = from i in dc.Operators where i.Salary >= n select i;

        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();


        Page.RegisterStartupScript("open", "<script>alert(' Greater Than and Equal Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";

    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        var n = Convert.ToInt32(TextBox5.Text);

        // use for salary Less Than and Equal  given salary

        var v = from i in dc.Operators where i.Salary <= n select i;

        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();


        Page.RegisterStartupScript("open", "<script>alert('Less Than and Equal Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";

    }
}

)




No comments:

Post a Comment