แก้ไข

แชร์ผ่าน


LinqDataSource.Insert(IDictionary) Method

Definition

Performs an insert operation.

public:
 int Insert(System::Collections::IDictionary ^ values);
public int Insert (System.Collections.IDictionary values);
member this.Insert : System.Collections.IDictionary -> int
Public Function Insert (values As IDictionary) As Integer

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.

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();
}
Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim listDictionary As New System.Collections.Specialized.ListDictionary()
    listDictionary.Add("ProductName", TextBox1.Text)
    listDictionary.Add("ProductCategory", "General")
    listDictionary.Add("Color", "Not assigned")
    listDictionary.Add("ListPrice", Nothing)
    LinqDataSource1.Insert(listDictionary)

    TextBox1.Text = String.Empty
    DetailsView1.DataBind()
End Sub

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" />
<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