Tuesday, December 8, 2015

WPF Form (Assignment 12)

Here is XAML

<Window x:Class="Assignment12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Assignment12"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label x:Name="label" Content="Enter the base Amount" HorizontalAlignment="Left" Margin="49,0,0,0" VerticalAlignment="Top" Height="27" Width="194"/>
        <TextBox x:Name="txtAmount" HorizontalAlignment="Left" Height="23" Margin="210,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="134"/>
        <RadioButton x:Name="rdbTenPercent" Content="Ten Percent" HorizontalAlignment="Left" Margin="63,45,0,0" VerticalAlignment="Top"/>
        <RadioButton x:Name="rdbFifteenPercent" Content="fifteen Percent" HorizontalAlignment="Left" Margin="63,65,0,0" VerticalAlignment="Top"/>
        <RadioButton x:Name="rdbTwentyPercent" Content="Twenty Percent" HorizontalAlignment="Left" Margin="63,85,0,0" VerticalAlignment="Top"/>
        <RadioButton x:Name="rdbOther" Content="Other" HorizontalAlignment="Left" Margin="63,105,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtOther" HorizontalAlignment="Left" Height="23" Margin="210,103,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="btnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="63,141,0,0" VerticalAlignment="Top" Width="75" Click="btnCalculate_Click"/>
        <Label x:Name="lblResult" Content="Label" HorizontalAlignment="Left" Margin="210,126,0,0" VerticalAlignment="Top" Height="97" Width="154"/>
        <Button x:Name="btnClear" Content="Clear" HorizontalAlignment="Left" Margin="63,175,0,0" VerticalAlignment="Top" Width="75" Click="btnClear_Click"/>
        <Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" Margin="63,208,0,0" VerticalAlignment="Top" Width="75" Click="btnExit_Click"/>

    </Grid>
</Window>

Here is the Tip Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment12
{
    class Tip
    {
       public double Amount { get; set; }
        public double TipPercent { get; set; }

        private const double TAX = .092;

        public double CalculateTip()
        {
            return Amount * TipPercent;
        }

        public double CalculateTax()
        {
            return Amount * TAX;
        }

        public double CalculateTotal()
        {
            return Amount + (Amount * TipPercent)
                + (Amount * TAX);
        }

        public override string ToString()
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Amount: " + Amount.ToString("$#,###.00"));
            sb.AppendLine("Tip: " + CalculateTip().ToString("$#,###.00"));
            sb.AppendLine("Tax: " + CalculateTax().ToString("$#,###.00"));
            sb.AppendLine("Total: " + CalculateTotal().ToString("$#,###.00"));
            return sb.ToString();
        }
    }
}

Here is the code behind the Form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Assignment12
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            Calculate();
        }

        private void Calculate()
        {
            Tip t = new Tip();
            double amount;
            bool goodAmount = double.TryParse(txtAmount.Text, out amount);
            if (goodAmount)
            {
                t.Amount = amount;
            }
            else
            {
                MessageBox.Show("Enter a valid Amount");
                txtAmount.Clear();
                txtAmount.Focus();
                return;
            }
            t.TipPercent = GetTipPercent();

            lblResult.Content = t.ToString();

        }

        private double GetTipPercent()
        {
            double tipPercent = 0;
            if (rdbTenPercent.IsChecked == true)
                tipPercent = .1;
            if (rdbFifteenPercent.IsChecked == true)
                tipPercent = .15;
            if (rdbTwentyPercent.IsChecked == true)
                tipPercent = .2;
            if (rdbOther.IsChecked == true)
            {
                bool goodPercent = double.TryParse(txtOther.Text, out tipPercent);
                if (tipPercent > 1)
                    tipPercent /= 100;
            }
            return tipPercent;

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            Clear();
        }

        private void Clear()
        {
            txtAmount.Clear();
            txtOther.Clear();
            rdbTenPercent.IsChecked = false;
            rdbFifteenPercent.IsChecked = false;
            rdbTwentyPercent.IsChecked = false;
            rdbOther.IsChecked = false;
            lblResult.Content = "";
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            //this.Close();
            Application.Current.Shutdown();
        }
    }
}

Monday, December 7, 2015

Web page for Donors

the SQL for the DonorLogin

use Master;
Create Login DonorLogin with password='pass';
Use communityAssist;
Create user DonorLogin for login DonorLogin;
Create Role DonorRole;
Grant select, insert on Person to DonorRole;
Grant select, insert on PersonAddress to DonorRole;
Grant select, insert on PersonContact to DonorRole;
Grant select, insert on Donation to DonorRole;

exec sp_AddRoleMember 'DonorRole', 'DonorLogin'

Here is the asp.net code for the web page

<%@ 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>Donor Registration</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Donor Registration</h1>
        <table>
            <tr>
                <td>Enter First Name</td>
                <td>
                    <asp:TextBox ID="FirstNameTextBox" runat="server">
                    </asp:TextBox>

                </td>
                </tr>
            <tr>
                <td>Enter Last Name</td>
                <td>
                    <asp:TextBox ID="LastNameTextBox" runat="server">
                    </asp:TextBox>

                </td>
                </tr>
            <tr>
                <td>Enter Street Address</td>
                <td>
                    <asp:TextBox ID="StreetTextBox" runat="server">
                    </asp:TextBox>

                </td>
                </tr>
            <tr>
                <td>Enter Email</td>
                <td>
                    <asp:TextBox ID="EmailTextBox" runat="server">
                    </asp:TextBox>

                </td>
                </tr>
            <tr>
                <td>Enter password</td>
                <td>
                    <asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password">
                    </asp:TextBox>

                </td>
                </tr>
            <tr>
                <td>
                    <asp:Button ID="SaveDonor" runat="server" Text="Button" OnClick="SaveDonor_Click" /></td>
                <td>
                    <asp:Label ID="ErrorLabel" runat="server" Text="Label"></asp:Label>

                </td>
            </tr>

        </table>
    </div>
    </form>
</body>
</html>

Here is the C# code with the caveat that we never got to run it, and you would need to add all the parameters to make it work.

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;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["loggedInUser"] == null)
        {
            Response.Redirect("Login.aspx");
        }
    }

    protected void SaveDonor_Click(object sender, EventArgs e)
    {
        SqlConnection connect =
            new SqlConnection(ConfigurationManager.
            ConnectionStrings["CommunityAssistConnection"].ToString());
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connect;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "usp_NewDonorLogin";
        cmd.Parameters.AddWithValue("@lastName", LastNameTextBox.Text);
        cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text);

        connect.Open();
        cmd.ExecuteNonQuery();
        connect.Close();

    }
}

And here is the web config file with the connections string

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
<connectionStrings>
  <add connectionString="Data source=.\sqlexpress; initial catalog=communityAssist; user=DonorLogin; password=pass"
       name="communityAssistConnection"/>
</connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>

</configuration>