Shivprasad Koirala
.NET/ASP.NET interview Questions - Using LINQ show us insert, update and delete operation..
By Shivprasad Koirala in ASP.NET on May 31 2011
  • Shivprasad Koirala
    May, 2011 31

    Answer:

    Let us assume that we have the following table of Student with their respective fields.

     
    StudentId
     
    int
     
    StudentName
     
    nvarchar(50)
     
    StudentAdd
     
    nvarchar(50)

    First open visual studio > New > project > select ASP.NET empty web application.

    Add > New item > select Web Form and create a simple form like below.

     

    L1.JPG

    Once you have done creating the form, now you have to add LINQ to SQL Classes in your project.

    Add > New item > select LINQ to SQL Classes.

    As soon as you click on add a new window will appear from that just click on server explorer expand the data connection and select your database then drag and drop the table on the .dbml file like below diagram.

    L2.JPG

    Now you have to add a class library file in your project.

    Add > New item > select Class.

    usingSystem.Data.Linq;
    usingSystem.Data.Linq.Mapping;namespaceLinq_to_insert
    {[Table(Name="Student")]
    publicclassClass1{
    privatestring _Name;
    privatestring _Add;publicstring Name{ 
    get{ _Name = value;}                                                  
    get{return _Name;}
            }
    publicstring Add{
    set{ _Add = value; }
    get{return _Add;}}}
    }

    Write code for insert update and delete with their respective buttons.

    For adding record.

     

    protectedvoidbtnAdd_Click(object sender, EventArgs e){
    DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
    StudentObjStud = newStudent();
    ObjStud.StudentAdd = TextBox2.Text;
    ObjStud.StudentName = TextBox1.Text.ToString();
    ObjDataContext.GetTable().InsertOnSubmit(ObjStud);
    ObjDataContext.SubmitChanges();
    Response.Write("Record Successfully Inserted");}

    For updating record.
     
    protectedvoidbtnUpdate_Click(object sender, EventArgs e){
    DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
    StudentObjStud = newStudent();
    string Name1 = TextBox1.Text;
    string Address = TextBox2.Text;
    var Update = ObjDataContext.Students.Single(p =>p.StudentName == Name1);
    Update.StudentAdd = Address;
    ObjDataContext.SubmitChanges();
    Response.Write("Record Successfully Updated");}

    For deleting record.
     
    protectedvoidbtnDelete_Click(object sender, EventArgs e){
    DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
    StudentObjStud = newStudent();
    string Name1 = TextBox1.Text.ToString();
    var delete = ObjDataContext.Students.Single(s =>s.StudentName == Name1);
    ObjDataContext.Students.DeleteOnSubmit(delete);
    ObjDataContext.SubmitChanges();
    Response.Write("Record Successfully deleted");}

     

    You will be also interested in watching the below video, which are also asked in most of the interviews and favourable question of interviewers.


     
    Please click here to see more .NET and ASP.NET interview questions

    Regards,

    Visit Authors blog for more.NET and ASP.NET interview Question
     
     

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS