Home > Articles > Programming > ASP .NET

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Inserting Data with LINQ to SQL

In this final section, we’ll examine how you can use LINQ to SQL to modify database data. In this section, you’ll build a page that you can use to insert new records into the Products database table.

When using LINQ to SQL, you insert new records by calling the InsertOnSubmit() method. After calling the InsertOnSubmit() method, you must call SubmitChanges() to make the insertion happen. The SubmitChanges() method executes all of the database commands that have been queued. The modified Product class in Listing 7 illustrates how to write a method that inserts new products into the Products database table.

Listing 7 Product.cs with Insert method.

using System;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;

public partial class Product
{

  public IEnumerable<Product> Select()
  {
    StoreDataContext db = new StoreDataContext();
    return from p in db.Products select p;
  }

  public void Insert(Product newProduct)
  {
    StoreDataContext db = new StoreDataContext();
    db.Products.InsertOnSubmit(newProduct);
    db.SubmitChanges();
  }

}

The ASP.NET page in Listing 8 uses the Product class. The page contains a GridView control and a FormView control. When you enter a new product with the FormView control, the product is added to the database and displayed in the GridView (see Figure 8).

Listing 8 ShowInsert.aspx.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Show Insert</title>
</head>
<body>
  <form id="form1" runat="server">

  <fieldset>
  <legend>New Product</legend>

  <asp:FormView
    id="frm"
    DataSourceID="srcProducts"
    DefaultMode="Insert"
    runat="server">
    <InsertItemTemplate>

    <asp:Label
      id="lblName"
      Text="Name:"
      AssociatedControlID="txtName"
      Runat="server" />
    <br />
    <asp:TextBox
      id="txtName"
      Text=’<%# Bind("Name") %>’
      Runat="server" />

    <br /><br />

    <asp:Label
      id="lblPrice"
      Text="Price:"
      AssociatedControlID="txtPrice"
      Runat="server" />
    <br />
    <asp:TextBox
      id="txtPrice"
      Text=’<%# Bind("Price") %>’
      Runat="server" />

    <br /><br />

    <asp:Button
      id="btnInsert"
      Text="Insert Product"
      CommandName="Insert"
      Runat="server" />

    </InsertItemTemplate>
  </asp:FormView>
  </fieldset>

  <br /><br />

  <asp:GridView
    id="grd"
    DataSourceID="srcProducts"
    Runat="server" />

  <asp:ObjectDataSource
    id="srcProducts"
    TypeName="Product"
    DataObjectTypeName="Product"
    SelectMethod="Select"
    InsertMethod="Insert"
    Runat="server" />

  </form>
</body>
</html>

To keep things simple, I’ve left out any validation from the page in Listing 8. In real life, at the very least, you would want to associate RequiredFieldValidator controls with both the txtName and txtPrice TextBox controls.

Figure 8

Figure 8 Inserting new records with LINQ to SQL.

Notice how much code you avoid writing when using LINQ to SQL. You don’t need to get your hands dirty by touching any ADO.NET objects. You never need to set up a database connection or command. All of the low-level plumbing is handled for you in the background by LINQ to SQL.

  • + Share This
  • 🔖 Save To Your Account