Introduction :
In this article we are going to create a WPF application for simple login and registration process.
Step 1.
Open Visual Studio 2010 -> File -> New -> Project.
New project template will display and select WPF Application like as follows:
Give your project a meaningful name, I am naming my project as “CustomerRelationManagement”
Step 2.
Here you will get MainWindow.xaml window, you can rename it to Login.xaml
Step 3.
Code in app.xaml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="conn"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Step 4.
Login.xaml
<Window x:Class="CustomerRelationManagement.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Login" Height="400" Width="500" WindowStartupLocation="CenterScreen">
<Grid Height="390" Width="475">
<TextBox Height="23" HorizontalAlignment="Left" Margin="130,104,0,0" Name="txtemail" VerticalAlignment="Top" Width="246" BorderBrush="#FF1A0202" />
<PasswordBox Height="23" HorizontalAlignment="Left" Margin="130,144,0,0" Name="txtpassword" VerticalAlignment="Top" Width="246" BorderBrush="#FF1A0202" />
<Label Content="Email ID :" Height="28" HorizontalAlignment="Left" Margin="55,99,0,0" Name="label1" VerticalAlignment="Top" Foreground="#FF0062FF" FontSize="13" FontWeight="Bold" />
<Label Content="Password :" Height="28" HorizontalAlignment="Left" Margin="48,139,0,0" Name="label2" VerticalAlignment="Top" Foreground="#FF0062FF" FontSize="13" FontWeight="Bold" />
<Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="130,192,0,0" Name="button1" VerticalAlignment="Top" Width="246" Click="button1_Click" />
<Label Content="Sample Login Form" Height="35" HorizontalAlignment="Left" Margin="83,27,0,0" Name="label3" VerticalAlignment="Top" Width="282" HorizontalContentAlignment="Center" Foreground="Red" FontSize="15" FontWeight="SemiBold" />
<Label Content="New User" FontSize="13" FontWeight="Bold" Foreground="#FF0062FF" Height="28" HorizontalAlignment="Left" Margin="207,257,0,0" Name="label4" VerticalAlignment="Top" />
<Button Content="Register Here" Height="23" HorizontalAlignment="Left" Margin="282,262,0,0" Name="button2" VerticalAlignment="Top" Width="122" Click="button2_Click" />
</Grid>
</Window>
Step 5.
Login.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace CustomerRelationManagement
{
/// <summary>
/// Interaction logic for Login.xaml
/// </summary>
public partial class Login : Window
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
public Login()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Register reg = new Register();
reg.Show();
this.Hide();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from employee where email = '" + txtemail.Text.Trim() + "' and password = '" + txtpassword.Password.Trim() + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Welcome wel = new Welcome(dt.Rows[0]["name"].ToString());
wel.Show();
this.Hide();
}
else
{
MessageBox.Show("Invalid Details.");
}
}
}
}
Step 6.
Register.xaml
<Window x:Class="CustomerRelationManagement.Register"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Register" Height="500" Width="500" WindowStartupLocation="CenterScreen">
<Grid Height="480" Width="480">
<Label Content="Sample Registration Form" FontSize="15" FontWeight="SemiBold" Foreground="Red" Height="35" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="71,27,0,0" Name="label3" VerticalAlignment="Top" Width="282" />
<TextBox BorderBrush="#FF1A0202" Height="23" HorizontalAlignment="Left" Margin="126,97,0,0" Name="txtemail" VerticalAlignment="Top" Width="246" />
<Label Content="Email ID :" FontSize="13" FontWeight="Bold" Foreground="#FF0062FF" Height="28" HorizontalAlignment="Left" Margin="51,92,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox BorderBrush="#FF1A0202" Height="23" HorizontalAlignment="Left" Margin="126,139,0,0" Name="txtname" VerticalAlignment="Top" Width="246" />
<Label Content="Name :" FontSize="13" FontWeight="Bold" Foreground="#FF0062FF" Height="28" HorizontalAlignment="Left" Margin="66,134,0,0" Name="label2" VerticalAlignment="Top" />
<PasswordBox BorderBrush="#FF1A0202" Height="23" HorizontalAlignment="Left" Margin="126,180,0,0" Name="txtpassword" VerticalAlignment="Top" Width="246" />
<Label Content="Password :" FontSize="13" FontWeight="Bold" Foreground="#FF0062FF" Height="28" HorizontalAlignment="Left" Margin="44,177,0,0" Name="label4" VerticalAlignment="Top" />
<Button Content="Register" Height="23" HorizontalAlignment="Left" Margin="126,230,0,0" Name="btnreg" VerticalAlignment="Top" Width="246" Click="button1_Click" />
<Button Content="Back to Login" Height="23" HorizontalAlignment="Left" Margin="12,296,0,0" Name="button2" VerticalAlignment="Top" Width="122" Click="button2_Click" />
</Grid>
</Window>
Step 7.
Register.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;
using System.Configuration;
using System.Data.SqlClient;
namespace CustomerRelationManagement
{
/// <summary>
/// Interaction logic for Register.xaml
/// </summary>
public partial class Register : Window
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
public Register()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Login log = new Login();
log.Show();
this.Hide();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (txtemail.Text.Trim() != "" && txtname.Text.Trim() != "" && txtpassword.Password.Trim() != "")
{
try
{
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("proc_register", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@email", txtemail.Text.Trim());
cmd.Parameters.AddWithValue("@name", txtname.Text.Trim());
cmd.Parameters.AddWithValue("@password", txtpassword.Password.Trim());
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Registration Successfull.");
Login log = new Login();
log.Show();
this.Hide();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
else
{
MessageBox.Show("Please fill all details");
}
}
}
}
Step 8.
Welcome.xaml
<Window x:Class="CustomerRelationManagement.Welcome"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Welcome" Height="300" Width="500" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
<Grid>
<Label Content="Label" Height="33" HorizontalAlignment="Left" Margin="100,95,0,0" Name="label1" VerticalAlignment="Top" Width="303" />
</Grid>
</Window>
Step 9.
Welcome.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;
namespace CustomerRelationManagement
{
/// <summary>
/// Interaction logic for Welcome.xaml
/// </summary>
public partial class Welcome : Window
{
static string nm;
public Welcome(string name)
{
InitializeComponent();
nm = name;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
label1.Content = "Welcome " + nm;
}
}
}
Step 10.
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="conn"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Step 11.
Add service base database model to your project, in my case I have named it as Database1.mdf
And create a table named employee as
And a stored procedure named proc_register as
ALTER PROCEDURE dbo.proc_register
@email nvarchar(max), @name nvarchar(max), @password nvarchar(max)
AS
begin
if not exists(select * from employee where email <> @email)
begin
insert into employee(name, email, password)
values(@name, @email, @password)
end
end
No comments:
Post a Comment