Tuesday, March 13, 2012

A Front End Example

this is ugly but it show briefly how coding a front end would work. this page is for the employees, when a car comes in they would enter the car's license plate and the program would bring up the car information and all the past services

First here is a picture of the program running

Here is the code for the html and ASP xml Default.aspx


<%@ 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>
        <asp:Label ID="Label1" runat="server" Text="Enter License Plate"></asp:Label><asp:TextBox
            ID="txtLicense" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="GetVehicle" 
            onclick="Button1_Click" />
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <asp:GridView ID="GridView2" runat="server" BackColor="LightGoldenrodYellow" 
            BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" 
            GridLines="None">
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
            <FooterStyle BackColor="Tan" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
                HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <SortedAscendingCellStyle BackColor="#FAFAE7" />
            <SortedAscendingHeaderStyle BackColor="#DAC09E" />
            <SortedDescendingCellStyle BackColor="#E1DB9C" />
            <SortedDescendingHeaderStyle BackColor="#C2A47B" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Here is the C# code Default.aspx.cs


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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //connect to the database
        //pass it the sql
        //retreive the results
        SqlConnection connect = new SqlConnection
        ("Data source=Localhost;initial catalog=automart;integrated security=true");

        string sql = "Select VehicleID, LastName, Firstname, LicenseNumber, "
        + "VehicleMake, VehicleYear From Customer.Vehicle "
        + "inner Join Person "
        + "on Person.PersonKey=Vehicle.PersonKey "
        + "Where LicenseNumber = @License";

        SqlCommand cmd = new SqlCommand(sql, connect);
        cmd.Parameters.AddWithValue("@License", txtLicense.Text);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        da.Fill(ds);

        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();

        string sql2 = "Select LocationName, ServiceDate, ServiceTime, "
            + " ServiceName From Customer.Location loc"
            + " Inner Join Employee.VehicleService vs "
            + " on loc.LocationID = vs.LocationID "
            + "inner join Employee.VehicleServiceDetail vsd "
            + " on vs.VehicleServiceID=vsd.VehicleServiceID "
            + " inner Join Customer.AutoService a "
            + "on a.AutoServiceID=vsd.AutoServiceID "
            + "Where VehicleID=@VehicleID";

        SqlCommand cmd2 = new SqlCommand(sql2, connect);
        int vehID = 0;
        foreach(DataRow row in ds.Tables[0].Rows)
        {
            vehID = int.Parse(row["VehicleID"].ToString());
        }

        cmd2.Parameters.AddWithValue("@VehicleID",vehID);

        SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
        da2.Fill(ds, "Services");

        GridView2.DataSource = ds.Tables["Services"];
        GridView2.DataBind();

    }
}

No comments:

Post a Comment