Xpode.com        Click here to Print this article.

.NET/ASP.NET interview Questions - With the help of LINQ do simple insert, update and delete.

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
 
 



http://
http://

Contributed by:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=484

Click here to go on website