.NET/ASP.NET interview Questions – LINQ operation – Insert, Update and Delete (Post this question on yahoo groups)

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
Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s