Friday, June 20, 2014

LINQ Insert Update Delete

ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Class1 obj = new Class1();
        obj.insert(txtname.Text, txtaddress.Text);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Class1 obj = new Class1();
        obj.delete(Convert.ToInt32(txtid.Text));
    }
}
Class file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Configuration;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    public string aaa = ConfigurationManager.ConnectionStrings["nareshConnectionString"].ToString();
    DataClassesDataContext aaaa = new DataClassesDataContext();


    public Class1()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public void insert(string name, string address)
    {
        T_Linq obj = new T_Linq();
        //obj.id = id;
        obj.name = name;
        obj.address = address;
        aaaa.T_Linqs.InsertOnSubmit(obj);
        aaaa.SubmitChanges();
    }
    public void delete(int id)
    {
        T_Linq objid = (from p in aaaa.T_Linqs
                          where p.id == id
                          select p).SingleOrDefault();
        objid.id = id;
        aaaa.T_Linqs.DeleteOnSubmit(objid);
        aaaa.SubmitChanges();
    }
}