Monday, 11 June 2012

insert,update Delete through LINQ.

What is Linq ?
LINQ or Language-Integrated Query is such a tool. LINQ is set of extensions to the .Net Framework 3.5 and its managed languages that sets the query as an object. It defines a common syntax and a programming model to query different types of data using a common language.

Advantages of Using LINQ

 

There are four main advantages of LINQ. They are,
The availability of strong typed queries: The classes are auto generated according to the relations in relational databases. The language is very much easy to understand as in SQL.
The automatic join functioning in foreign keys: In normal SQL, the user has to join the tables if it is necessary. In LINQ, it provides the ability to join each function automatically when there is a foreign key reference.
The code size: There are many occasions that the users have to write long sentences for getting a SQL query. LINQ provides relatively short codes in such advanced occasions. It reduces the complexity of the code and makes it much easy for the program to read.
Code equality: One of the most advantages in using LINQ is that its availability over any .NET platform language such as C#.net, VB.NET and F#.NET.
1.First Rifgclick in your Website Name.
2.Right click on the Web site.
3.Add Linq to Sql Classes
4.Add in your Table in Sqlclass  page.For Example -:









5.Next Add a class File in your Application.Means In your Table field Name.For Example

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

/// <summary>
/// Summary description for LinqClass
/// </summary>
public class LinqClass
{
    public int LedgerID
    {
        get;
        set;
    }
    public string LedgerName
    {
        get;
        set;
    }
    public int CompanyId
    {
        get;
        set;
    }
}

Source Code Design

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
      <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" Width="27px"></asp:TextBox><br />
       
          <asp:Label ID="Label2" runat="server" Text="ID"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
      <asp:Button ID="btn_save" runat="server" Text="Save" onclick="btn_save_Click" />
        <asp:Button ID="btn_delete" runat="server" Text="Delete"
            onclick="btn_delete_Click" />
        <asp:Button ID="btn_update" runat="server" Text="Update"
            onclick="btn_update_Click" />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
        <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
</asp:Content>

Page Design





C# 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;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter da;
    DataSet ds;
    SqlCommand cmd;
    DataTable dt;
    public string conectionn = ConfigurationManager.ConnectionStrings["Satya"].ConnectionString;
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        connection();
        if (!IsPostBack)
        {
            Details();
        }
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        SatyaDataClassesDataContext salinq = new SatyaDataClassesDataContext(conectionn);
        int i = int.Parse(TextBox1.Text);
        Rakesh rr = salinq.Rakeshes.Single(p => p.LedgerID == i);
        salinq.Rakeshes.DeleteOnSubmit(rr);
        salinq.SubmitChanges();
        Label3.Text = "Success";
        Details();
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        SatyaDataClassesDataContext satsave = new SatyaDataClassesDataContext(conectionn);
        Rakesh ra = new Rakesh();
        ra.LedgerName = TextBox1.Text;
        ra.CompanyID = int.Parse(TextBox2.Text);
        satsave.Rakeshes.InsertOnSubmit(ra);
        satsave.SubmitChanges();
        Label3.Text = "Success";
        Details();

    }

    protected void btn_update_Click(object sender, EventArgs e)
    {
        SatyaDataClassesDataContext satupdate = new SatyaDataClassesDataContext(conectionn);
        int i = int.Parse(TextBox3.Text);
        Rakesh ra = satupdate.Rakeshes.Single(p=> p.LedgerID==i);
        ra.LedgerName = TextBox1.Text;
        ra.CompanyID = int.Parse(TextBox2.Text);
        satupdate.SubmitChanges();
        Label3.Text = "Success";
        Details();
    }
    #endregion
    #region Methods
    public void connection()
    {
        con = new SqlConnection(conectionn);
    }

    // Show Data In GridView Start
    public void Details()
    {
        var ggg = from hh in GetDetails()
                  select hh;
        connection();
        GridView1.DataSource = ggg.ToList();
        GridView1.DataBind();
    }
    public List<LinqClass> GetDetails()
    {
        List<LinqClass> objDetails = new List<LinqClass>();
        connection();
        dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Rakesh",con);
        da.Fill(dt);
        foreach (DataRow dr in dt.Rows)
        {
            LinqClass ob = new LinqClass();
            ob.LedgerID = int.Parse(dr.Field<int>("LedgerID").ToString());
            ob.LedgerName = dr.Field<string>("LedgerName").ToString();
            ob.CompanyId =int.Parse(dr.Field<int>("CompanyID").ToString());
            objDetails.Add(ob);
        }
        return objDetails;
    }
    //  END
    #endregion

}

Table Design

2 comments:

  1. I read your post interesting and informative. I am doing research on bloggers who use effectively blog for disseminate information.My Thesis titled as "Study on Blogging Pattern Of Selected Bloggers(Indians)".I glad if u wish to participate in my research.Please contact me through mail. Thank you

    ReplyDelete