Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

LinqDataSource.Update Method

Performs an update operation.

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

public int Update(
	IDictionary keys,
	IDictionary values,
	IDictionary oldValues
)

Parameters

keys
Type: System.Collections.IDictionary

The row key values for the records to be updated.

values
Type: System.Collections.IDictionary

The row values to be updated in the data source.

oldValues
Type: System.Collections.IDictionary

The row values that are evaluated to detect data conflicts.

Return Value

Type: System.Int32
The number of records affected by the update operation.

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

The following example shows how to programmatically update a record in the data source after the user clicks a button. The code passes a ListDictionary object that contains the key value, a ListDictionary object that contains the original values, and a ListDictionary object that contains the new values to the Update method.

protected void Reset_Click(object sender, EventArgs e)
{
    ListDictionary keyValues = new ListDictionary();
    ListDictionary newValues = new ListDictionary();
    ListDictionary oldValues = new ListDictionary();

    keyValues.Add("ProductID", int.Parse(((Label)DetailsView1.FindControl("IDLabel")).Text));

    oldValues.Add("ProductName", ((Label)DetailsView1.FindControl("NameLabel")).Text);
    oldValues.Add("ProductCategory", ((Label)DetailsView1.FindControl("CategoryLabel")).Text);
    oldValues.Add("Color", ((Label)DetailsView1.FindControl("ColorLabel")).Text);

    newValues.Add("ProductName", "New Product");
    newValues.Add("ProductCategory", "General");
    newValues.Add("Color", "Not assigned");

    LinqDataSource1.Update(keyValues, newValues, oldValues);

    DetailsView1.DataBind();
}

The following example shows the markup for the previous example.

        <asp:LinqDataSource  
          ContextTypeName="ExampleDataContext" 
          TableName="Products" 
          EnableUpdate="true" 
          ID="LinqDataSource1" 
          runat="server">
        </asp:LinqDataSource>
        <asp:DetailsView 
          DataSourceID="LinqDataSource1" 
          AllowPaging="True" 
          ID="DetailsView1" 
          runat="server" 
          AutoGenerateRows="False">
          <Fields>
            <asp:templatefield HeaderText="Product ID">
				<itemtemplate>
					<asp:Label ID="IDLabel" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
				</itemtemplate>
			</asp:templatefield>
          	<asp:templatefield HeaderText="Product Name">
				<itemtemplate>
					<asp:Label ID="NameLabel" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
				</itemtemplate>
			</asp:templatefield>
          	<asp:templatefield HeaderText="Category">
				<itemtemplate>
					<asp:Label ID="CategoryLabel" runat="server" Text='<%# Bind("ProductCategory") %>'></asp:Label>
				</itemtemplate>
			</asp:templatefield>
			<asp:templatefield HeaderText="Color">
				<itemtemplate>
					<asp:Label ID="ColorLabel" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
				</itemtemplate>
			</asp:templatefield>
          </Fields>
        </asp:DetailsView>
        <asp:button 
          ID="Button1"
          Text="Reset with default values" 
          runat="server" 
		  onclick="Reset_Click" />

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5
Show:
© 2015 Microsoft