Mega Code Archive

 
Categories / ASP.Net Tutorial / Data Binding
 

SqlDataSource with Insert and Update parameters

<%@ Page Language="C#" AutoEventWireup="true" %> <!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>Untitled Page</title> </head> <body>     <form id="form1" runat="server">         &nbsp;         <div>             <asp:GridView ID="GridView1"                            runat="server"                            AutoGenerateColumns="False"                            DataKeyNames="ProductID"                           DataSourceID="SqlDataSource1"                            EmptyDataText="There are no data records to display.">                 <Columns>                     <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />                     <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />                     <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" />                     <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />                     <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />                     <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />                     <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />                     <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />                     <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" SortExpression="ReorderLevel" />                     <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />                 </Columns>             </asp:GridView>             <asp:SqlDataSource ID="SqlDataSource1"                                 runat="server"                                 ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"                 DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued)"                 ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>"                 SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued] FROM [Products]"                 UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [SupplierID] = @SupplierID, [CategoryID] = @CategoryID, [QuantityPerUnit] = @QuantityPerUnit, [UnitPrice] = @UnitPrice, [UnitsInStock] = @UnitsInStock, [UnitsOnOrder] = @UnitsOnOrder, [ReorderLevel] = @ReorderLevel, [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID">                 <InsertParameters>                     <asp:Parameter Name="ProductName" Type="String" />                     <asp:Parameter Name="SupplierID" Type="Int32" />                     <asp:Parameter Name="CategoryID" Type="Int32" />                     <asp:Parameter Name="QuantityPerUnit" Type="String" />                     <asp:Parameter Name="UnitPrice" Type="Decimal" />                     <asp:Parameter Name="UnitsInStock" Type="Int16" />                     <asp:Parameter Name="UnitsOnOrder" Type="Int16" />                     <asp:Parameter Name="ReorderLevel" Type="Int16" />                     <asp:Parameter Name="Discontinued" Type="Boolean" />                 </InsertParameters>                 <UpdateParameters>                     <asp:Parameter Name="ProductName" Type="String" />                     <asp:Parameter Name="SupplierID" Type="Int32" />                     <asp:Parameter Name="CategoryID" Type="Int32" />                     <asp:Parameter Name="QuantityPerUnit" Type="String" />                     <asp:Parameter Name="UnitPrice" Type="Decimal" />                     <asp:Parameter Name="UnitsInStock" Type="Int16" />                     <asp:Parameter Name="UnitsOnOrder" Type="Int16" />                     <asp:Parameter Name="ReorderLevel" Type="Int16" />                     <asp:Parameter Name="Discontinued" Type="Boolean" />                     <asp:Parameter Name="ProductID" Type="Int32" />                 </UpdateParameters>                 <DeleteParameters>                     <asp:Parameter Name="ProductID" Type="Int32" />                 </DeleteParameters>             </asp:SqlDataSource>          </div>     </form> </body> </html> File: Web.config <?xml version="1.0"?> <configuration>   <appSettings/>   <connectionStrings>   <add name="NorthwindConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"    providerName="System.Data.SqlClient" />  </connectionStrings>   <system.web>     <compilation debug="true"/>     <authentication mode="Windows"/>   </system.web> </configuration>