C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

LINQ DataSource Insert, Update and Delete : ASP.NET 4.0


Posted Date: 22 Jul 2009    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Satish Kumar JMember Level: Diamond    
Rating: 1 out of 5Points: 20



Note: Screen Shots refered in this article attached with this Article.

In this example I am trying to display list of products in a GridView and Insert, Update and Delete products using LINQ Datasource, so lets start building the application.

Create a new Web Application Project in Visual Studio and add one GridView and LINQ DataSource to

Default.aspx, before we configure LINQ data source we need a LINQ to SQL Class so add a new file to project

and select LINQ to SQL class file.

Refere Screen Shot: Design.JPG
Refere Screen Shot: LINQToSQL.JPG



Once this file is added project we need to select data from data base, for this open the server explorer and

Drag and drop Products, Supplier and Category Tables to this file, please refer below screen shot.

Refere Screen Shot: LINQToSQL1.JPG


Now we have successfully created LINQ SQL class with our required entities, to reflect these changes to

project we need build project once here.

Now open Default.aspx and select LINQDataSource1 controls and click ‘>’ symbol to configure the datasource,

once you click on that symbol you will be asked to select the Entity class, as we have already created our

LINQ to SQL class you can select that from the combo box
Refere Screen Shot: ConfigureLINQ1.JPG


Click on Next to select the actual entity that is products, here we need the option to Select, Update and

delete so click on Advance button and select those options.

Refere Screen Shot: ConfigureLINQ2.JPG


Once selected click on Ok, and Finish, now our LINQDatasource is bound with data and ready to bind to

GridView, so select the GridView and click ‘>’ symbol to configure datasource. Select DataSource as

LINQDataSource1 and check the options Enable Editing and Enable Deleting.

Refere Screen Shot: ConfigureGridView.JPG



Now we are ready with our Display products, Edit Products and Delete products you can run the project and

check these are working fine.

Refere Screen Shot: Output1.jpg



Now we will add a new product for that we need a separate web form where we will add new product, for this

add new Web for and name it as AddProduct.aspx, now add 6 TextBoxs, 2 DropDownLists, 2 Buttons and one

CheckBox control to this page as show in the below Screen shot, also add 3 LINQDataSource objects to form.

Refere Screen Shot: AddProductDesign.JPG



Here we need 3 LINQDataSource controls because we want to show Categories and Suppliers in combo boxes while

creating the product so that user will select the values, so select LinqDataSource1 control and configure

select SQL to LINQ class entity and select Suppliers as Table and select only SupplierId and CompanyName

fields.

Refere Screen Shot: SuppliersLINQ.JPG


Similarly select LinqDataSource2 and configure and select Categories Table and select only CategoryId and

CategoryName fields from this.

Refere Screen Shot: CategoryLINQ.JPG



These two linq datasources are for displaying the data now we will configure LinqDataSource3 which is for

inserting the Product details, so select the LinqDataSource3 and configure select Product table and all

fields and select Advanced button and select the option LINQ to perform automatic insert

Refere Screen Shot: ProductInsertLINQ.JPG


Now bind the Category and Supplier data to 2 DropDownLists using LinqDataSource1 and LinqDataSource2.

Refere Screen Shot: BindDDL1.jpg


Similarly for Categories DropDownList, now we have to insert the data into LinqDataSource3 once user enters

data and clicks Save button, so we need to write following code in the Save button’s click event


protected void Button1_Click(object sender, EventArgs e)
{
ListDictionary lstNewValues = new ListDictionary();
lstNewValues.Add("ProductName", TextBox1.Text);
lstNewValues.Add("SupplierID", DropDownList1.SelectedValue);
lstNewValues.Add("CategoryID", DropDownList2.SelectedValue);
lstNewValues.Add("QuantityPerUnit", TextBox4.Text);
lstNewValues.Add("UnitPrice", TextBox5.Text);
lstNewValues.Add("UnitsInStock", TextBox6.Text);
lstNewValues.Add("UnitsOnOrder", TextBox7.Text);
lstNewValues.Add("ReorderLevel", TextBox8.Text);
lstNewValues.Add("Discontinued", CheckBox1.Checked);
LinqDataSource3.Insert(lstNewValues);
Response.Redirect("Default.aspx");
}

protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}


In the above code I am creating a ListDictionary object and adding all the values with the Column names and

passing that object to Insert method of LinqDatasource after inserting I am redirecting to Default.aspx

page. Also on back button click I am redirecting to Default.aspx

So to launch Add Product page we need to add a link on the Default.aspx page and on clicking link redirect

to AddProduct.aspx page.

Now we are ready with our code let build, run and test.


Refere Screen Shot: Output2.jpg
Refere Screen Shot: Output3.jpg




Hope this helps

Tags : LINQDataSource Insert, Update and Delete, ASP.NET 4.0, GridView, LINQ


SatishKumar J
Microsoft MVP(ASP.NET)


Attachments

  • Screen shots (30675-22344-ScreenShots.zip)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Update and Delete  .  LINQDataSource Insert  .  LINQ  .  GridView  .  ASP.NET 4.0  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Open the files from Asp.net
    Previous Resource: Inserting Multiple Records in a single insert Statement
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET/Web Applications


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use