Export (0) Print
Expand All

LinqDataSource.Insert Method

Performs an insert operation.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public int Insert(
	IDictionary values
)

Parameters

values
Type: System.Collections.IDictionary

The row values to be inserted into the data source.

Return Value

Type: System.Int32
The number of rows affected by the insert operation.

Typically, you do not have to call the Insert method from your code. The data-bound control will automatically call the Insert method when the user takes action to insert a new record. You explicitly call the Insert method when you want to create your own process for inserting data.

The following example shows how to programmatically insert a new record in the data source after the user clicks a button. The code passes a ListDictionary object that contains default values to the Insert method.

protected void Add_Click(object sender, EventArgs e)
{
    System.Collections.Specialized.ListDictionary listDictionary
        = new System.Collections.Specialized.ListDictionary();
    listDictionary.Add("ProductName", TextBox1.Text);
    listDictionary.Add("ProductCategory", "General");
    listDictionary.Add("Color", "Not assigned");
    listDictionary.Add("ListPrice", null);
    LinqDataSource1.Insert(listDictionary);

    TextBox1.Text = String.Empty;
    DetailsView1.DataBind();
}

The following example shows the markup for the previous example.

        <asp:LinqDataSource 
          ContextTypeName="ExampleDataContext" 
          TableName="Products" 
          EnableInsert="true" 
          ID="LinqDataSource1" 
          runat="server">
        </asp:LinqDataSource>
        <asp:DetailsView 
          DataSourceID="LinqDataSource1" 
          AllowPaging="true" 
          ID="DetailsView1" 
          runat="server">
        </asp:DetailsView>
        New product name:<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:button ID="Button1" 
          Text="Add new product with default values" 
          runat="server" 
		  onclick="Add_Click" />

.NET Framework

Supported in: 4.5, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft