SqlDataSourceView.InsertParameters Property
Gets the parameters collection containing the parameters that are used by the InsertCommand property.
Assembly: System.Web (in System.Web.dll)
[PersistenceModeAttribute(PersistenceMode.InnerProperty)] public ParameterCollection InsertParameters { get; }
Property Value
Type: System.Web.UI.WebControls.ParameterCollectionA ParameterCollection that contains the parameters used by the InsertCommand property.
If the InsertCommand property contains a parameterized SQL query, the InsertParameters collection contains any Parameter objects that correspond to the parameter placeholders in the SQL string.
Depending on the ADO.NET provider, the order of the parameters in the InsertParameters collection might be important. The System.Data.OleDb and System.Data.Odbc providers associate the parameters in the collection according to the order in which the parameters appear in the parameterized SQL query. The System.Data.SqlClient provider, which is the default ADO.NET provider for the SqlDataSource control, associates the parameters in the collection by matching the name of the parameter with a placeholder alias in the SQL query. For more information on parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.
The following code example demonstrates how to insert data into a database using the SqlDataSource control and a simple Web Forms page. The current data in the table is displayed in the DropDownList control. You can add new records by entering values in the TextBox controls, and then clicking the Insert button. When the Insert button is clicked, the specified values are inserted into the database and the DropDownList is refreshed.
<%@Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> private void InsertShipper (object source, EventArgs e) { SqlDataSource1.Insert(); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="form1" runat="server"> <asp:dropdownlist id="DropDownList1" runat="server" datasourceid="SqlDataSource1" datatextfield="CompanyName" datavaluefield="ShipperID" /> <!-- Security Note: The SqlDataSource uses a FormParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the FormParameter, handle the Inserting event. --> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind %>" selectcommand="SELECT CompanyName,ShipperID FROM Shippers" insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)"> <insertparameters> <asp:formparameter name="CoName" formfield="CompanyNameBox" /> <asp:formparameter name="Phone" formfield="PhoneBox" /> </insertparameters> </asp:sqldatasource> <br /><asp:textbox id="CompanyNameBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="CompanyNameBox" Display="Static" ErrorMessage="Please enter a company name." /> <br /><asp:textbox id="PhoneBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="PhoneBox" Display="Static" ErrorMessage="Please enter a phone number." /> <br /><asp:button id="Button1" runat="server" text="Insert New Shipper" onclick="InsertShipper" /> </form> </body> </html>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.