Tuesday, March 19, 2013

Web Page Examples

First we did the wizard to get all the products. Here is the source code for Default.aspx<>p>

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Our Products</h1>
        <asp:DataList ID="DataList1" runat="server" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333">
            <AlternatingItemStyle BackColor="White" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <ItemStyle BackColor="#E3EAEB" />
            <ItemTemplate>
                <strong>
                <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /></strong>
                <br />
                $
                <asp:Label ID="ProductUnitPriceLabel" runat="server" Text='<%# Eval("ProductUnitPrice") %>' />
                <br />

            </ItemTemplate>
            <SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PerfectPizzaConnectionString %>" SelectCommand="SELECT [ProductName], cast([ProductUnitPrice] as decimal(5,2)) as [ProductUnitPrice] FROM [Product] ORDER BY [ProductName]"></asp:SqlDataSource>
    </div>
        <p>Enter your phone number to order or register if you are a new customer  <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
        </p>
        <asp:LinkButton ID="LinkButton1" runat="server">Register</asp:LinkButton>
    </form>
</body>
</html>

Here is the code behind which checks to see if the phone number matches

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 Button1_Click(object sender, EventArgs e)
    {
        PerfectPizzaEntities pe = new PerfectPizzaEntities();
        var ph = from p in pe.Customers
                 where p.CustomerPhoneKey.Equals(txtPhone.Text)
                 select p.CustomerPhoneKey;
        if (ph.ToList().Count != 0)
            Response.Redirect("Default3.aspx");
        else
            Response.Redirect("Default4.aspx");
                
    }
}

Here is the form for entering a new customer

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Register</h1>
        <table>
            <tr>
                <td>Enter phone</td>
                <td>
                    <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Last Name</td>
                <td>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Address1</td>
                <td>
                    <asp:TextBox ID="txtAddress1" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Address2</td>
                <td>
                    <asp:TextBox ID="txtAddress2" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>City</td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>State</td>
                <td>
                    <asp:TextBox ID="txtState" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Zip Code</td>
                <td>
                    <asp:TextBox ID="txtZip" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /></td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

And here is the code behind

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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        PerfectPizzaEntities pe = new PerfectPizzaEntities();
        Customer c = new Customer();
        c.CustomerPhoneKey = txtPhone.Text;
        c.CustomerLastName = txtLastName.Text;
        c.CustomerAddress1 = txtAddress1.Text;
        c.CustomerAddress2 = txtAddress2.Text;
        c.CustomerCity = txtCity.Text;
        c.CustomerState = txtState.Text;
        c.CustomerZip = txtZip.Text;
        pe.Customers.Add(c);
        pe.SaveChanges();

        Response.Redirect("Default2.aspx");
    }
}

Wednesday, March 6, 2013

Logins and users

--admin sql
--authentication--are you who you say you are
 --login to the server
 --login mapped to a user at the database level
 --user is given database permissions
 --windows authentication
 --sql server authentication --user name password
--authorization--what can do

--Here is a windows login
Use Master
USE [master]
GO

/****** Object:  Login [NT AUTHORITY\SYSTEM]    Script Date: 3/6/2013 11:26:39 AM ******/
CREATE LOGIN [NT AUTHORITY\SYSTEM] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
GO

--a sql server login
Create login EmployeeLogin with password='P@ssw0rd1', default_database=CommunityAssist

Use CommunityAssist
--schema are collections of objects
Go
Create schema EmployeeSchema 
--create a user that maps to that login and uses the schema
Create user EmployeeUser for Login EmployeeLogin with default_schema=employeeschema

Create role EmployeeRole

--assign permissions to the role
Grant select on Donation to EmployeeRole
Grant Select on Employee to EmployeeRole
Grant update on Donation to EmployeeRole
Grant exec on usp_newDonor to EmployeeRole
Grant exec on usp_ReturnDonorInfo to EmployeeRole

--assign the user to the role
exec sp_addrolemember 'employeerole', 'employeeUser'

Select * from PersonContact

Go
--create an object that belongs to the schema
Create view EmployeeSchema.GrantsView
As
Select ServiceName, sum(GrantAmount)as Total
From [Service]
inner Join ServiceGrant
on [Service].ServiceKey=ServiceGrant.ServiceKey
Group by ServiceName
go
Grant Select on schema::EmployeeSchema to Employeeuser


Monday, March 4, 2013

XML

use Automart

Select * from Customer.AutoService

use CommunityAssist

Select * From Person
For xml raw('person'), elements, root('People')

Select lastName, FirstName, Contactinfo
From Person 
inner join PersonContact 
On person.PersonKey=personcontact.PersonKey
For xml auto, elements, root('people')

Create xml Schema Collection meetingNotesSchema
As
'<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.communityAssist.com/meetingNotes" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="meetingNote">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="heading">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="meetingDate" type="xs:string" />
              <xs:element name="attending">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="member" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="subject" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="body">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="notes" type="xs:string" />
              <xs:element name="tasks">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="taskName" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>'

Create table Meeting
(
 MeetingID int identity(1,1) Primary Key,
 MeetingNote xml(meetingNotesSchema)
)

Insert into Meeting(MeetingNote)
Values('<?xml version="1.0" encoding="utf-8"?>
<meetingNote xmlns="http://www.communityAssist.com/meetingNotes" >
  <heading>
    <meetingdate>3/4/2013</meetingdate>
    <attending>
      <member>George Jetson </member>
      <member>Mark Hammel</member>
      <member>Carie Fisher</member>
    </attending>
    <subject>Star Wars</subject>
  </heading>
  <body>
    <notes>
      We met to talk about our starwars promotion for something or other.
    </notes>
    <tasks>
      <taskName>Get the news out</taskName>
      <taskName>Mind meld</taskName>
    </tasks>
  </body>
</meetingNote>')


Select * from Meeting

use Automart

Select ServiceName, ServiceDescription.query('declare namespace sd="http://www.automart.com/servicedescription"; sd:servicedescription/sd:parts/sd:part') as parts
 from customer.AutoService
 Where ServiceName='Replace fuel pump'

 Select ServiceName, ServiceDescription.query('declare namespace sd="http://www.automart.com/servicedescription"; sd:servicedescription/sd:description') as [Description]
 from customer.AutoService
 Where ServiceName='Replace fuel pump'