Click here to Skip to main content
15,892,927 members
Articles / Web Development / ASP.NET

Using the Insert feature of SqlDataSource with Gridviews

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
29 Mar 20073 min read 112.2K   1.7K   28  
Explains insert command and the advantages and disadvantages of the insert feature of .NET 2.0's SqlDataSource
<%@ 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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]"></asp:SqlDataSource>
    
    </div>
        <asp:GridView ID="GridView1" PageSize="3" HeaderStyle-BackColor="MediumBlue" HeaderStyle-ForeColor="white" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >
            <Columns>
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                <asp:TemplateField HeaderText="Orders">
                    <ItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                            SelectCommand="SELECT [OrderID], [OrderDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)" 
                            DeleteCommand="DELETE FROM ORDERS WHERE ([OrderID] = @OrderID)" 
                            InsertCommand="INSERT INTO ORDERS (CustomerID,OrderDate)VALUES(@CustomerID,GETDATE())" 
                            UpdateCommand="UPDATE Orders SET [OrderDate] = @OrderDate WHERE ([CustomerID] = @CustomerID)">
                            <SelectParameters>
                                <asp:Parameter Name="CustomerID" Type="String" />
                            </SelectParameters>
                            <DeleteParameters>
                                <asp:Parameter Name="OrderID" />
                            </DeleteParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="OrderDate" />
                                <asp:Parameter Name="CustomerID" />
                            </UpdateParameters>
                            <InsertParameters>
                                <asp:Parameter Name="CustomerID" />
                            </InsertParameters>
                        </asp:SqlDataSource>
                        <asp:GridView ID="GridView2" HeaderStyle-BackColor="dodgerblue" HeaderStyle-ForeColor="white" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderID" 
                            DataSourceID="SqlDataSource2">
                            <Columns>
                                <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True"
                                    SortExpression="OrderID" />
                                <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" />
                                <asp:CommandField ShowInsertButton="False" ShowEditButton="True" ShowDeleteButton="True" />
                            </Columns>
                        </asp:GridView>
                        <asp:Button ID="btnAddOrder" Text="Add Order" OnCommand="GridView1_AddOrder" CommandName="AddOrder" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"CustomerID")%>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle VerticalAlign="Top" />
        </asp:GridView>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions