Link DetailsView with SqlDataSource and do the editing : DetailsView « ADO.net Database « ASP.Net






Link DetailsView with SqlDataSource and do the editing


<%@ Page Language="C#"%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="sourceProducts" 
                           runat="server"
                           ProviderName="System.Data.SqlClient"
                           ConnectionString="<%$ ConnectionStrings:Northwind %>"
                           SelectCommand="SELECT ProductID, ProductName, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products"
                           UpdateCommand="UPDATE Products SET ProductName=@ProductName, UnitPrice=CONVERT(money, @UnitPrice), UnitsInStock=@UnitsInStock, UnitsOnOrder=@UnitsOnOrder, ReorderLevel=@ReorderLevel, Discontinued=@Discontinued WHERE ProductID=@ProductID"
                           DeleteCommand="DELETE Products WHERE ProductID=@ProductID"
                           InsertCommand="INSERT INTO Products (ProductName, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued) VALUES (@ProductName, CONVERT(money, @UnitPrice), @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued)">  
        </asp:SqlDataSource>
        <asp:DetailsView ID="DetailsView1" 
                         runat="server" 
                         Height="50px" 
                         Width="125px"  
                         DataKeyNames="ProductID"
                         AllowPaging="True" 
                         DataSourceID="sourceProducts"
                         AutoGenerateInsertButton="true" 
                         AutoGenerateDeleteButton="true" 
                         AutoGenerateEditButton="true">
            <PagerSettings PageButtonCount="25" />            
        </asp:DetailsView>
        <br />
    </div>
    </form>
</body>
</html>


File: Web.config


<?xml version="1.0"?>

<configuration>

  <connectionStrings>
    <add name="Northwind" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI"/>
  </connectionStrings>
</configuration>

 








Related examples in the same category

1.Using a PagerTemplate to customize the paging interface.
2.Editing a record with the DetailsView control.
3.Displaying a Master/Detail form with the DetailsView control.
4.Using a template when editing with the DetailsView control.
5.Handling Concurrency Issues: CompareAllValues or OverwriteChanges
6.Inserting Data with the DetailsView Control
7.Deleting Data with the DetailsView Control
8.Use the ItemInserted event to handle any errors
9.Adding an AutoGenerateInsertButton attribute to the DetailsView
10.Using the GridView and DetailsView together
11.Enabling paging on the DetailsView control
12.Customizing the display of the DetailsView control