LinqDataSource.Insert Method (IDictionary)

 

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
Available since 3.5
Return to top
Show: