Monday, April 29, 2013

Edit,delete,update records in gridview using sqldatasource in asp.net

Title: Edit Delete Update in Grid view in asp.net

Description:In previous post i have given how to edit,delete,update in grid view using row events,Bind data to grid view using SQL data source(here you can see how to use it).For this we need to set the  properties for grid view to enable the columns for db transaction.Might be get where the DML commands are executed?.The answer is the sqldatasource has properties to executes those commands when user perform those action on grid view

Example:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OrderGrid.aspx.cs" Inherits="OrderGrid" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GvOrder" AllowSorting="True" Runat="server"
AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" AutoGenerateColumns="False" DataSourceID="OrdrDb">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID"
SortExpression="OrderID" />
<asp:BoundField DataField="OrderName" HeaderText="OrderName"
SortExpression="OrderName" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="Amount" HeaderText="Amount"
SortExpression="Amount" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="OrdrDb" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Orders]" UpdateCommand="UPDATE [Orders] SET [OrderName] = @OrderName, [Phone] = @Phone,
[Address] = @Address, [Amount] = @Amount WHERE [OrderID] = @OrderID" DeleteCommand="Delete [Orders] WHERE [OrderID] = @OrderID"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Result:
Display Order table table using Data source:
Bind data to gridview
Update Records using edit operation in grid view





No comments:

Bel