Skip to main content

LINQ Basic Quires

In this Article we learn Basic LINQ Queries in Asp.Net .

1-      Fill Gridview record:
public void showgridview()
    {

        DataClassesDataContext dc = new DataClassesDataContext();

        var q =
        from a in dc.GetTable<bio>()
        select a;
        GridView1.DataSource = q;
        GridView1.DataBind();
   
   }


         

                 


2-      Search record:

public void SearchQuery()
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        var q =
        from a in dc.GetTable<bio>()
        where a.name == TextBox3.Text
        select a;
        GridView1.DataSource = q;
        GridView1.DataBind();

    }


                   


3-      Insert Record

  Public void InsertQuery()
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        bio obj = new bio();
        obj.name = TextBox3.Text;
        obj.passw = TextBox5.Text;
        obj.fname = TextBox4.Text;
        dc.bios.InsertOnSubmit(obj);
        dc.SubmitChanges();
        showgridview();
    }

                     


4-      Update record

public void UpdateQuery()
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        bio objbio = dc.bios.Single(bio => bio.name == TextBox3.Text);
        objbio.passw = TextBox5.Text;
        objbio.fname = TextBox4.Text;
        dc.SubmitChanges();

    }

                     

5-      Delete Record

public void DeleteQuery()
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        bio obb = dc.bios.Single(bio => bio.name == TextBox3.Text);
        dc.bios.DeleteOnSubmit(obb);
        dc.SubmitChanges();

    }

                    

6-      Inner Join
There is two table fee and bio.

Bio Table:

                            
                         


Fee Table:
                         



DataClassesDataContext dc = new DataClassesDataContext();
        var biofee = from bio in dc.bios
                     join fee in dc.fees
                     on bio.id equals fee.id
                     select new { bio.name, bio.fname, fee.Address };
        GridView1.DataSource = biofee;
        GridView1.DataBind();


//You can also do this with the help of lambda operator
// Inner joinning Usin lambda operator

        DataClassesDataContext dc = new DataClassesDataContext();
        var query = dc.bios.Join(dc.fees, r => r.id, p => p.id, (r, p) => new { r.name, r.fname, p.Address });
        GridView1.DataSource = query;
        GridView1.DataBind();



                            


Comments

Popular posts from this blog

Working With a Binding Navigator Control in Windows Forms

Definition : BindingNavigator control Provide user interface with a simple data navigation and manipulation on the form . it is used for Binding the Database Tables values to the control (Datagridview,Text Box, label etc.). BindingNavigator with the BindingSource enable users to move through data records on a form and interact with the records. There are few buttons in binding Navigator listed below: 1.       Move First--------------- à Go first record of the table 2.       Move Next--------------- à Go Next record of the table with respect to current record. 3.       Move Previous---------- à Go previous record of the table. 4.       Move Last---------------- à Go Last record of the table. 5.       Delete---------------------- à Delete selected row (record). 6.       Add New-------------------- à Add New Row. 7. ...

Autocomplete TextBox in Windows Form Application using SQL Server

Autocomplete TextBox in Windows Form Application using SQL Server. In this article we Lear how to create autocompelete textbox on the basis of sql server data. Step 1: First open visual Studio ---->File---->New---->Project----->Windows From Application---->Ok Drag and Drop label and TextBox. Step 2: Now Double Click on Windows Form and write the following Code :          SqlConnection  con =  new   SqlConnection ( @"Data Source=IS9;Initial Catalog=practice;User ID=sa;Password=abc" );          public   void  loadData()         {             con.Open();              AutoCompleteStringCollection  namesCollection =  new   AutoCompleteStringCollection ();  ...

Login Form Using LINQ in Windows Forms Application

Step 1:  First we create a database and then Table in SQL Server. Here we create a table named “logintb”. Now insert a name and Password into the logintb table. Step 2:  Open Visual Studio then select "File" -> "New" -> "Project..." then select "Windows Forms". Drag and drop a Label for ID and a Label for password. Drag and drop a TextBox for txtuser and a TextBox for txtPassword. Drag and drop a Button for Login. Step 3:  Go to the project then right-click and seelct "Add" -> "New Item..." then select "Windows Form" -> "LINQ to SQL Classes" then click on "Add". Now go to the Server Explorer, drag and drop the logintb table as in the following: Open Server Explorer and select the database and then the table. Step 4 :  Now double-click on the button Login in the form and write the following code: CS Code:   using  System; using  System.Collections.Generic; using  System.Comp...