Tech Junkie Blog - Real World Tutorials, Happy Coding!: ASP.NET SqlDataSource Part 3: INSERT New Record To GridView

Saturday, July 21, 2018

ASP.NET SqlDataSource Part 3: INSERT New Record To GridView

In this tutorial we will add the INSERT functionality to our GridView control.

Follow the steps below to enable insert on our GridView control.

1. Open the source view in the .aspx page and add the ShowInsertButton attribute to the <asp:CommandField element inside the <asp:GridView element

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:CommandField ShowInsertButton="True" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <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>



2. Click on the ">" icon on the SqlDataSource control to configure the datasource, then click on the "Configure Data Source" link

3.  Click next twice until you see the "Configure the Select Statement" screen, click on the "Advanced.." button.  Then check the "Generate INSERT, UPDATE, and DELETE statements" and the "Use optimistic concurrency" checkboxes. Then click "OK"





1 comment:

Search This Blog