LinqDataSource.Insert(IDictionary) Method

Definition

Performs an insert operation.

C#
public int Insert(System.Collections.IDictionary values);

Parameters

values
IDictionary

The row values to be inserted into the data source.

Returns

The number of rows affected by the insert operation.

Examples

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.

C#
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.NET (C#)
<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" />

Remarks

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.

Applies to

Produkt Verzie
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1