Thursday, July 22, 2010

wpf windows form

Here is the code for the Windows Presentation Foundation Form:

First here is the XAML

<Window x:Class="DonorWindows.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="575" Width="475">
<Grid Height="575" Width="475" Background="Cornsilk">
<Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="175" FontSize="16" Foreground="DarkBlue">Enter New Donation</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="24,62,0,0" Name="label2" VerticalAlignment="Top" Width="120">First Name</Label>
<TextBox Height="23" Margin="0,67,161,0" Name="txtFirstName" VerticalAlignment="Top" HorizontalAlignment="Right" Width="153" Background="PowderBlue" />
<Label Height="28" HorizontalAlignment="Left" Margin="24,107,0,0" Name="label3" VerticalAlignment="Top" Width="120">LastName</Label>
<TextBox Background="PowderBlue" Height="23" Margin="161,112,129,0" Name="txtLastName" VerticalAlignment="Top" />
<Label Height="28" HorizontalAlignment="Left" Margin="24,151,0,0" Name="label4" VerticalAlignment="Top" Width="120">Street</Label>
<TextBox Background="PowderBlue" Margin="161,151,129,0" Name="txtStreet" Height="23" VerticalAlignment="Top" />
<Label Height="28" HorizontalAlignment="Left" Margin="31,190,0,0" Name="label5" VerticalAlignment="Top" Width="120">Apartment</Label>
<TextBox Background="PowderBlue" Height="23" Margin="157,195,133,0" Name="txtApartment" VerticalAlignment="Top" />
<Label Height="28" HorizontalAlignment="Left" Margin="31,228,0,0" Name="label6" VerticalAlignment="Top" Width="120">City</Label>
<TextBox Background="PowderBlue" Height="23" Margin="157,233,133,0" Name="txtCity" VerticalAlignment="Top" />
<Label HorizontalAlignment="Left" Margin="31,272,0,255" Name="label7" Width="120">State</Label>
<TextBox Background="PowderBlue" Margin="157,277,0,276" Name="txtState" HorizontalAlignment="Left" Width="42" />
<Label Height="28" HorizontalAlignment="Left" Margin="31,0,0,217" Name="label8" VerticalAlignment="Bottom" Width="120">Zip Code</Label>
<TextBox Background="PowderBlue" Height="23" Margin="157,0,133,217" Name="txtZip" VerticalAlignment="Bottom" />
<Label Height="28" HorizontalAlignment="Left" Margin="24,0,0,175" Name="label9" VerticalAlignment="Bottom" Width="120">HomePhone</Label>
<TextBox Background="PowderBlue" Height="23" Margin="161,0,161,175" Name="txtHome" VerticalAlignment="Bottom" />
<Label Height="28" HorizontalAlignment="Left" Margin="24,0,0,135" Name="label10" VerticalAlignment="Bottom" Width="120">Work Phone</Label>
<TextBox Background="PowderBlue" Height="23" Margin="161,0,161,135" Name="txtWork" VerticalAlignment="Bottom" />
<Label Height="28" HorizontalAlignment="Left" Margin="31,0,0,95" Name="label11" VerticalAlignment="Bottom" Width="120">Email</Label>
<TextBox Background="PowderBlue" Height="23" Margin="168,0,154,95" Name="txtEmail" VerticalAlignment="Bottom" />
<Label Height="28" HorizontalAlignment="Left" Margin="24,0,0,56" Name="label12" VerticalAlignment="Bottom" Width="120">Donation</Label>
<TextBox Background="PowderBlue" Height="23" Margin="161,0,161,56" Name="txtDonation" VerticalAlignment="Bottom" />
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,42,52" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click">Submit</Button>
</Grid>
</Window>


Now here is the c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data;
using System.Data.SqlClient;

namespace DonorWindows
{
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
//connect to the database
//pass it some command any parameters
//deal with results

SqlConnection connect = new SqlConnection
("Data source=localhost;initial catalog=communityAssist;user=genericdonor;password=pass");

SqlCommand cmd = new SqlCommand();
cmd.Connection = connect;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "donor.usp_RegisterDonor";

cmd.Parameters.AddWithValue("@Lastname", txtLastName.Text);
cmd.Parameters.AddWithValue("@firstname", txtFirstName.Text);
cmd.Parameters.AddWithValue("@Street", txtStreet.Text);
cmd.Parameters.AddWithValue("@Apartment", txtApartment.Text);
cmd.Parameters.AddWithValue("@City", txtCity.Text);
cmd.Parameters.AddWithValue("@State", txtState.Text);
cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
cmd.Parameters.AddWithValue("@DonationAmount", double.Parse(txtDonation.Text));

if (txtHome.Text != "")
{
cmd.Parameters.AddWithValue("@HomePhone", txtHome.Text);
}

if (txtWork.Text != "")
{
cmd.Parameters.AddWithValue("@WorkPhone", txtWork.Text);
}

if (txtEmail.Text != "")
{
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
}

int result;
connect.Open();
result=(int)cmd.ExecuteNonQuery();
connect.Close();



if (result > 0)
{
MessageBox.Show( "Thank you for your donation");
}
else
{
MessageBox.Show( "there are problems with your donation");
}

}
}
}

Notes:

The only difference between this code and the web page code is the use of the MessageBox object and I changed the connection string to connect as generalDonor. We discovered a couple of things about connecting as generaldonor. One, the create table command in the trigger gave problems; two I had to assign insert permissions on the underlying tables for the stored procedure to work fully.

A better strategy might be to create the schema with its objects and then create a role that assigns all the permissions needed on those and other objects. then attach a user to both the role and the schema. We might look at this later.

No comments:

Post a Comment