<Window x:Class="Personform.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="Enter Last Name" Height="28" HorizontalAlignment="Left" Margin="53,35,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="189,35,0,0" Name="txtLastName" VerticalAlignment="Top" Width="120" />
<Label Content="Enter First Name" Height="28" HorizontalAlignment="Left" Margin="53,83,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="189,83,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="120" />
<Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="61,167,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
The Windows1.xaml.cs
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;
namespace Personform
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
Person person = new Person(txtLastName.Text, txtFirstName.Text);
PersonWrite personWrite = new PersonWrite(person);
txtFirstName.Clear();
txtLastName.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
The person class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Personform
{
class Person
{
private string first;
private string last;
public Person(string fName, string lName)
{
FirstName = fName;
LastName = lName;
}
public string FirstName
{
get { return first; }
set { first = value; }
}
public string LastName
{
get { return last; }
set { last = value; }
}
}
}
the Person Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Personform
{
class PersonWrite
{
private SqlConnection connect;
private Person pers;
public PersonWrite(Person p)
{
connect = new SqlConnection
("Data Source=localhost;initial catalog=CommunityAssist;integrated security=true");
pers = p;
WritePerson();
}
private void WritePerson()
{
try
{
string Sql = "Insert into Person(LastName, FirstName) Values (@last, @first)";
SqlCommand cmd = new SqlCommand(Sql, connect);
cmd.Parameters.AddWithValue("@last", pers.LastName);
cmd.Parameters.AddWithValue("@first", pers.FirstName);
connect.Open();
cmd.ExecuteNonQuery();
}
catch(SqlException)
{
Exception ex = new Exception("Your sql sucks");
throw ex;
}
catch (Exception ex)
{
throw ex;
}
finally
{
connect.Close();
}
}
}
}