I am trying to use a FormView to connect to my SQL DB, I am following a guide but on the guide it says that if I am in the ItemTemplate there should be a “New” button hyperlink to get into InsertItemTemplate, the problem I am facing is that the button does not exist. How would I get into insert mode so I can add a new record into my SQL db? And what is the syntax to actually adding the items into the database? Thank you for any help
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" CellPadding="4" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." ForeColor="#333333"
Height="250px" Width="957px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField ApplyFormatInEditMode="True" DataField="Date_Added"
DataFormatString="{0:MMM d, yyyy}" HeaderText="Date_Added" HtmlEncode="False"
SortExpression="Date_Added" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT [ID], [Title], [Description], [Date Added] AS Date_Added FROM [knowledgebase]">
</asp:SqlDataSource>
<br />
</div>
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
<EditItemTemplate>
ID:
<asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
<br />
Title:
<asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
<br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
<br />
Date_Added:
<asp:TextBox ID="Date_AddedTextBox" runat="server"
Text='<%# Bind("Date_Added") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Title:
<asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
<br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
<br />
Date_Added:
<asp:TextBox ID="Date_AddedTextBox" runat="server"
Text='<%# Bind("Date_Added") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Bind("Description") %>' />
<br />
Date_Added:
<asp:Label ID="Date_AddedLabel" runat="server"
Text='<%# Bind("Date_Added") %>' />
<br />
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>
To get into insertion mode, you must either include a control in your other template that has command name “New”, or provide some other input that can cause your codebehind to programmatically go
(yourFormView).ChangeMode(FormViewMode.Insert). Then in the insert item template, you need a button whose command is “Insert”, or a control that can programmatically go(yourDataSource).Insert(). Finally, your data source needs an InsertCommand whose value is the SQL to be executed, and some specification of parameters supplied to it, which can be done with the<InsertParameters>subtag, or programmatically in the DataSource_Inserting event handler.The first step gets you into the insertion mode of the formview, the second gets the form content sent back to the datasource, and the third gets the datasource to write it to the database.