Thursday, February 23, 2012

Inserting with Linq

First Drag the tables onto the LINQ Designer


Here is the Source code with the validation

<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>
        <asp:Label ID="Label1" runat="server" Text="Enter First Name"></asp:Label>
        <asp:TextBox ID="txtFirstname" runat="server"></asp:TextBox> <br />

         <asp:Label ID="Label2" runat="server" Text="Enter Last Name"></asp:Label>
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="txtLastName" Display="None" 
            ErrorMessage="Last Name is required"></asp:RequiredFieldValidator>
        <br />

         <asp:Label ID="Label3" runat="server" Text="Enter Street Address"></asp:Label>
        <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox> <br />

        <asp:Label ID="Label5" runat="server" Text="Enter City"></asp:Label>
        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox> <br />

         <asp:Label ID="Label4" runat="server" Text="Enter State (2 Characters) "></asp:Label>
        <asp:TextBox ID="txtState" runat="server"></asp:TextBox> <br />


         <asp:Label ID="Label6" runat="server" Text="Enter Zip Code"></asp:Label>
        <asp:TextBox ID="txtZip" runat="server"></asp:TextBox> 
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
            ControlToValidate="txtZip" Display="None" ErrorMessage="Invalid Zip Code" 
            ValidationExpression="\d{5}(-\d{4})?"></asp:RegularExpressionValidator>
        <br />

        <asp:Label ID="Label7" runat="server" Text="Enter Home Phone"></asp:Label>
        <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> <br />

        <asp:Label ID="Label8" runat="server" Text="Enter Email"></asp:Label>
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> 
        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
            ControlToValidate="txtEmail" Display="None" ErrorMessage="Invalid email" 
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
        <br />

        <asp:Label ID="Label9" runat="server" Text="Enter Donation Amount"></asp:Label>
        <asp:TextBox ID="txtDonation" runat="server"></asp:TextBox> 
        <asp:CompareValidator ID="CompareValidator1" runat="server" 
            ControlToValidate="txtDonation" Display="None" 
            ErrorMessage="Enter a valid Donation" Operator="DataTypeCheck" 
Type="Currency"></asp:CompareValidator>
        <br />

        <asp:Button ID="btnDonation" runat="server" Text="EnterDonation" 
            onclick="btnDonation_Click" />
    </p>
    </div>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
    </form>
</body>
</html>


Here is the LINQ code



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnDonation_Click(object sender, EventArgs e)
    {
        DonorDataContext dc = new DonorDataContext();
        Person p = new Person();
        p.LastName = txtLastName.Text;
        p.FirstName = txtFirstname.Text;

        dc.Persons.InsertOnSubmit(p);

        PersonAddress pa = new PersonAddress();
        pa.Person= p;
        pa.Street = txtStreet.Text;
        pa.City = txtCity.Text;
        pa.State = txtState.Text;
        pa.Zip = txtZip.Text;

        dc.PersonAddresses.InsertOnSubmit(pa);

        PersonContact pc = new PersonContact();
        pc.Person = p;
        pc.ContactTypeKey = 1;
        pc.ContactInfo = txtPhone.Text;

        dc.PersonContacts.InsertOnSubmit(pc);

        PersonContact pc2 = new PersonContact();
        pc2.Person = p;
        pc2.ContactTypeKey =6;
        pc2.ContactInfo = txtEmail.Text;

        dc.PersonContacts.InsertOnSubmit(pc2);

        Donation d = new Donation();
        d.Person = p;
        d.DonationDate = DateTime.Now;
        d.DonationAmount = decimal.Parse(txtDonation.Text);

        dc.Donations.InsertOnSubmit(d);

        dc.SubmitChanges();

    }
}

No comments:

Post a Comment