Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 20632 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Select, Update and Delete directly to the Database by Grid View using Linq Data Source

Select, Update and Delete directly to the Database by Grid View using Linq Data Source

Article posted by Gopesh9 on 8/4/2012 | Views: 3516 | Category: ASP.NET | Level: Beginner | Points: 250 red flag

Advertisements

Advertisements
We can direct select,update and delete the values of the database by Grid View using Linq Data Source.

Introduction


Linq Data Source is used to directly connect to the Sql Data Source. The LinqDataSource control exposes Language-Integrated Query (LINQ) to Web developers through the ASP.NET data-source control architecture 

Objective


At first add Linq To Sql Class, by right clicking on the solution Explorer. After adding that you have to add DataClasses.dbml in that add the table from which you want to link the grid view bu just drag and drop the table to the .dbml from the solution explorer.. 

After that add a LinqDataSource to the .aspx file on clicking on that add data souce that will automatically come on that. After that add the grid view and choose the datasource. Now you can select, insert and delete directly to the data source.  

Using the code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

</div>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"

ContextTypeName="DataClassesDataContext" EnableDelete="True"

EnableInsert="True" EnableUpdate="True" TableName="Bus_Details">

</asp:LinqDataSource>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"

AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="busno"

DataSourceID="LinqDataSource1">

<Columns>

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"

ShowSelectButton="True" />

<asp:BoundField DataField="travels" HeaderText="travels"

SortExpression="travels" />

<asp:BoundField DataField="busno" HeaderText="busno" ReadOnly="True"

SortExpression="busno" />

<asp:BoundField DataField="source" HeaderText="source"

SortExpression="source" />

<asp:BoundField DataField="destination" HeaderText="destination"

SortExpression="destination" />

<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />

<asp:BoundField DataField="time" HeaderText="time" SortExpression="time" />

<asp:BoundField DataField="category" HeaderText="category"

SortExpression="category" />

<asp:BoundField DataField="amount" HeaderText="amount"

SortExpression="amount" />

<asp:BoundField DataField="kilometers" HeaderText="kilometers"

SortExpression="kilometers" />

<asp:BoundField DataField="Mo" HeaderText="Mo" SortExpression="Mo" />

<asp:BoundField DataField="Tu" HeaderText="Tu" SortExpression="Tu" />

<asp:BoundField DataField="We" HeaderText="We" SortExpression="We" />

<asp:BoundField DataField="Th" HeaderText="Th" SortExpression="Th" />

<asp:BoundField DataField="Fr" HeaderText="Fr" SortExpression="Fr" />

<asp:BoundField DataField="Sa" HeaderText="Sa" SortExpression="Sa" />

<asp:BoundField DataField="Su" HeaderText="Su" SortExpression="Su" />

</Columns>

</asp:GridView>

</form>

</body>

</html>

Below is the way to configure the LinqDataSource control.

image009.jpg

Like this you can Configure the Linq Data Source. This will open a windows form like this.

 

After that By clicking on Next you will get...

And by Clicking to the Finish Button the Building of the linq data Source is Finished.
 
After that add LinqDataSource to the GridView.
 

Now by Enabling Paging, Selecting,Editing and Deleting you can update,select and delete directly to the database using GridView

Conclusion


You can do many more thing using Linq. Its very useful for connecting to the database.
Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About G. S.

Experience:0 year(s)
Home page:http://www.dotnetfunda.com
Member since:Friday, August 03, 2012
Level:Starter
Status: [Member]
Biography:A Dot Net Learner. Currently working in Software Company.
>> Write Response - Respond to this post and get points
Related Posts

Here i have tried to discuss Master Page handling concept in details.

When we see around, architectures mainly discuss about loose coupling , scalability , performance etc etc. Many architecture forget one of the important aspects in software is making application globalized. Depending on project some application would not really want to have multi-language based websites , but i am sure many will. So in this article we will go through a series of FAQ which will give you a quick start on making application multi-language based.

As you all know LINQ (Language Integrated Query) is the new way to communicate with database that has been introduced in .NET Framework 3.5. In this article, I am going to explain you how to work with LINQ to SQL Classes and LinqDataSource control. For this, you will need .NET Framework 3.5, Visual Web Developer 2008, and SqlServer Express Database.

Various options to convert Business Entity Class as XML

This example, can be taken aas code snippet, gives idea how to restrict user to upload a file/image within upload limit.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 5:24:34 AM