Login And Registration Process In WPF application

Introduction

In this article I am creating a simple application for login and registration using WPF in visual studio 2010. In this application I am creating two window forms one is for Registration and another is for login. First of all user will register after then he/she can login.

Now I am going to discuss in brief about this application. 

Step1: Open Visual Studio 2010 -> File -> New -> Project.

New project template will display and select WPF Application like as follows:

WPF Application

Figure 1:

Step 2: Enter your project name and click on OK button. 

Step 3: You will get the MainWindow.xaml window form, if you want to change then rename it and also change the StartupUri property of application into App.xaml like as follows:

App.xaml

  1. <Application x:Class="Login_WPF.App"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  3.              StartupUri="Registration.xaml">    
  4.     <Application.Resources></Application.Resources>    
  5. </Application>    
Step 4: Now design your Registration page as figure 2 or copy and page following inline code. 

Registration.xaml

  1. <Window x:Class="Login_WPF.Registration"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="Registration" Height="387" Width="528" Background="Black">  
  5.     <Grid  Height="350" Width="525" Background="Bisque">  
  6.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="textBlockHeading" Text="Registration:" VerticalAlignment="Top" Width="110"  FontSize="17" FontStretch="ExtraCondensed"/>  
  7.         <!--Button as a Link button using style-->  
  8.         <Button Margin="451,5,12,288" Content="Login" Cursor="Hand" Click="Login_Click">  
  9.             <Button.Template>  
  10.                 <ControlTemplate TargetType="Button">  
  11.                     <TextBlock TextDecorations="Underline">  
  12.                     <ContentPresenter />  
  13.                     </TextBlock>  
  14.                 </ControlTemplate>  
  15.             </Button.Template>  
  16.             <Button.Style>  
  17.                 <Style TargetType="Button">  
  18.                     <Setter Property="Foreground" Value="Navy" />  
  19.                     <Style.Triggers>  
  20.                         <Trigger Property="IsMouseOver" Value="true">  
  21.                             <Setter Property="Foreground" Value="Red" />  
  22.                         </Trigger>  
  23.                     </Style.Triggers>  
  24.                 </Style>  
  25.             </Button.Style>  
  26.         </Button>  
  27.         <!--end Button as a Link button using style-->  
  28.         <Grid Margin="31,0,29,23" Background="White" Height="264" VerticalAlignment="Bottom">  
  29.             <Grid.RowDefinitions>  
  30.                 <RowDefinition Height="252*" />  
  31.                 <!--   <RowDefinition Height="12*" />-->  
  32.             </Grid.RowDefinitions>  
  33.             <TextBlock Height="20" HorizontalAlignment="Left" Margin="67,0,0,0" x:Name ="errormessage" VerticalAlignment="Top" Width="247"  OpacityMask="Crimson" Foreground="#FFE5572C" />  
  34.             <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,20,0,0" Name="textBlockFirstname" Text="First Name:" VerticalAlignment="Top" Width="110" />  
  35.             <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,50,0,0" Name="textBlockLastName" Text="Last Name:" VerticalAlignment="Top" Width="110" />  
  36.             <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,80,0,0" Name="textBlockEmailId" Text="EmailId" VerticalAlignment="Top" Width="110" />  
  37.             <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,107,0,0" Name="textBlockPassword" Text="Password:" VerticalAlignment="Top" Width="110"  />  
  38.             <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,136,0,0" Name="textBlockConfirmPwd" Text="ConfirmPassword:" VerticalAlignment="Top" Width="110" Grid.RowSpan="2" />  
  39.             <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,166,0,0" Name="textBlockAddress" Text="Address" VerticalAlignment="Top" Width="110" />  
  40.             <TextBox Height="23" HorizontalAlignment="Left" Margin="183,20,0,0" Name="textBoxFirstName" VerticalAlignment="Top" Width="222" />  
  41.             <TextBox Height="23" HorizontalAlignment="Left" Margin="183,50,0,0" Name="textBoxLastName" VerticalAlignment="Top" Width="222" />  
  42.             <TextBox Height="23" HorizontalAlignment="Left" Margin="183,80,0,0" Name="textBoxEmail" VerticalAlignment="Top" Width="222" />  
  43.             <PasswordBox Height="23" HorizontalAlignment="Left" Margin="183,107,0,0" Name="passwordBox1" VerticalAlignment="Top" Width="222" />  
  44.             <!--For password-->  
  45.             <PasswordBox Height="23" HorizontalAlignment="Left" Margin="183,136,0,0" Name="passwordBoxConfirm" VerticalAlignment="Top" Width="222" />  
  46.             <TextBox Height="23" HorizontalAlignment="Left" Margin="183,0,0,66" Name="textBoxAddress" VerticalAlignment="Bottom" Width="222" />  
  47.             <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="183,204,0,0" Name="Submit" VerticalAlignment="Top" Width="70" Click="Submit_Click" />  
  48.             <Button Content="Reset" Height="23" HorizontalAlignment="Left" Margin="259,204,0,0" Name="button2" VerticalAlignment="Top" Width="70" Click="button2_Click" />  
  49.             <Button Content="Cancel" Height="23" HorizontalAlignment="Right" Margin="0,204,60,0" Name="button3" VerticalAlignment="Top" Width="70" Click="button3_Click" />  
  50.         </Grid>  
  51.     </Grid>  
  52. </Window>  

registration in wpf

Figure 2: Registration form 

Registration.xaml.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Shapes;  
  13. using System.Data;  
  14. using System.Data.SqlClient;  
  15. using System.Text.RegularExpressions;  
  16. namespace Login_WPF  
  17. {  
  18.     /// <summary>  
  19.     /// Interaction logic for Registration.xaml  
  20.     /// </summary>  
  21.     public partial class Registration : Window  
  22.     {  
  23.         public Registration()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.         private void Login_Click(object sender, RoutedEventArgs e)  
  28.         {  
  29.             Login login = new Login();  
  30.             login.Show();  
  31.             Close();  
  32.         }  
  33.         private void button2_Click(object sender, RoutedEventArgs e)  
  34.         {  
  35.             Reset();  
  36.         }  
  37.         public void Reset()  
  38.         {  
  39.             textBoxFirstName.Text = "";  
  40.             textBoxLastName.Text = "";  
  41.             textBoxEmail.Text = "";  
  42.             textBoxAddress.Text = "";  
  43.             passwordBox1.Password = "";  
  44.             passwordBoxConfirm.Password = "";  
  45.         }  
  46.         private void button3_Click(object sender, RoutedEventArgs e)  
  47.         {  
  48.             Close();  
  49.         }  
  50.         private void Submit_Click(object sender, RoutedEventArgs e)  
  51.         {  
  52.             if (textBoxEmail.Text.Length == 0)  
  53.             {  
  54.                 errormessage.Text = "Enter an email.";  
  55.                 textBoxEmail.Focus();  
  56.             }  
  57.             else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))  
  58.             {  
  59.                 errormessage.Text = "Enter a valid email.";  
  60.                 textBoxEmail.Select(0, textBoxEmail.Text.Length);  
  61.                 textBoxEmail.Focus();  
  62.             }  
  63.             else  
  64.             {  
  65.                 string firstname = textBoxFirstName.Text;  
  66.                 string lastname = textBoxLastName.Text;  
  67.                 string email = textBoxEmail.Text;  
  68.                 string password = passwordBox1.Password;  
  69.                 if (passwordBox1.Password.Length == 0)  
  70.                 {  
  71.                     errormessage.Text = "Enter password.";  
  72.                     passwordBox1.Focus();  
  73.                 }  
  74.                 else if (passwordBoxConfirm.Password.Length == 0)  
  75.                 {  
  76.                     errormessage.Text = "Enter Confirm password.";  
  77.                     passwordBoxConfirm.Focus();  
  78.                 }  
  79.                 else if (passwordBox1.Password != passwordBoxConfirm.Password)  
  80.                 {  
  81.                     errormessage.Text = "Confirm password must be same as password.";  
  82.                     passwordBoxConfirm.Focus();  
  83.                 }  
  84.                 else  
  85.                 {  
  86.                     errormessage.Text = "";  
  87.                     string address = textBoxAddress.Text;  
  88.                     SqlConnection con = new SqlConnection("Data Source=TESTPURU;Initial Catalog=Data;User ID=sa;Password=wintellect");  
  89.                     con.Open();  
  90.                     SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con);  
  91.                     cmd.CommandType = CommandType.Text;  
  92.                     cmd.ExecuteNonQuery();  
  93.                     con.Close();  
  94.                     errormessage.Text = "You have Registered successfully.";  
  95.                     Reset();  
  96.                 }  
  97.             }  
  98.         }  
  99.     }  
  100. }

Now I am taking another form for login as follows:

Login.xaml

  1. <Window x:Class="Login_WPF.Login"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  3.         Title="Login" Height="350" Width="525">  
  4.     <Grid>  
  5.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="LoginHeading" Text="Login:" VerticalAlignment="Top" FontSize="17" FontStretch="ExtraCondensed"/>  
  6.         <TextBlock Height="50" HorizontalAlignment="Left" Margin="24,48,0,0" Name="textBlockHeading" VerticalAlignment="Top" FontSize="12" FontStyle="Italic" Padding="5">  
  7.             Note: Please login here to view the features of this site. If you are new on this site then <LineBreak /><!--line break-->  
  8.             please click on  
  9.            <!--textblock as a Hyperlink.-->  
  10.             <TextBlock>  
  11.                  <Hyperlink Click="buttonRegister_Click" FontSize="14" FontStyle="Normal">Register</Hyperlink>  
  12.             </TextBlock>  
  13.             <!--end textblock as a hyperlink-->  
  14.             button  
  15.         </TextBlock>  
  16.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="66,120,0,0" Name="textBlock1" Text="Email" VerticalAlignment="Top" Width="67" />  
  17.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="58,168,0,0" Name="textBlock2" Text="Password" VerticalAlignment="Top" Width="77" />  
  18.         <TextBox Height="23" HorizontalAlignment="Left" Margin="118,115,0,0" Name="textBoxEmail" VerticalAlignment="Top" Width="247" />  
  19.         <PasswordBox Height="23" HorizontalAlignment="Left" Margin="118,168,0,0" Name="passwordBox1" VerticalAlignment="Top" Width="247" />  
  20.         <Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="118,211,0,0" Name="button1" VerticalAlignment="Top" Width="104" Click="button1_Click" />  
  21.         <TextBlock Height="23" HorizontalAlignment="Left" x:Name ="errormessage" VerticalAlignment="Top" Width="247" Margin="118,253,0,0"  OpacityMask="Crimson" Foreground="#FFE5572C"  />  
  22.     </Grid>  
  23. </Window>

login in wpf
 

Figure 3: Login form.

Login.xaml.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14. using System.Data;  
  15. using System.Data.SqlClient;  
  16. using System.Text.RegularExpressions;  
  17. namespace Login_WPF  
  18. {  
  19.     /// <summary>  
  20.     /// Interaction logic for MainWindow.xaml  
  21.     /// </summary>   
  22.     public partial class Login : Window  
  23.     {  
  24.         public Login()  
  25.         {  
  26.             InitializeComponent();  
  27.         }  
  28.         Registration registration = new Registration();  
  29.         Welcome welcome = new Welcome();  
  30.         private void button1_Click(object sender, RoutedEventArgs e)  
  31.         {  
  32.             if (textBoxEmail.Text.Length == 0)  
  33.             {  
  34.                 errormessage.Text = "Enter an email.";  
  35.                 textBoxEmail.Focus();  
  36.             }  
  37.             else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))  
  38.             {  
  39.                 errormessage.Text = "Enter a valid email.";  
  40.                 textBoxEmail.Select(0, textBoxEmail.Text.Length);  
  41.                 textBoxEmail.Focus();  
  42.             }  
  43.             else  
  44.             {  
  45.                 string email = textBoxEmail.Text;  
  46.                 string password = passwordBox1.Password;  
  47.                 SqlConnection con = new SqlConnection("Data Source=TESTPURU;Initial Catalog=Data;User ID=sa;Password=wintellect");  
  48.                 con.Open();  
  49.                 SqlCommand cmd = new SqlCommand("Select * from Registration where Email='" + email + "'  and password='" + password + "'", con);  
  50.                 cmd.CommandType = CommandType.Text;  
  51.                 SqlDataAdapter adapter = new SqlDataAdapter();  
  52.                 adapter.SelectCommand = cmd;  
  53.                 DataSet dataSet = new DataSet();  
  54.                 adapter.Fill(dataSet);  
  55.                 if (dataSet.Tables[0].Rows.Count > 0)  
  56.                 {  
  57.                     string username = dataSet.Tables[0].Rows[0]["FirstName"].ToString() + " " + dataSet.Tables[0].Rows[0]["LastName"].ToString();  
  58.                     welcome.TextBlockName.Text = username;//Sending value from one form to another form.  
  59.                     welcome.Show();  
  60.                     Close();  
  61.                 }  
  62.                 else  
  63.                 {  
  64.                     errormessage.Text = "Sorry! Please enter existing emailid/password.";  
  65.                 }  
  66.                 con.Close();  
  67.             }  
  68.         }  
  69.         private void buttonRegister_Click(object sender, RoutedEventArgs e)  
  70.         {  
  71.             registration.Show();  
  72.             Close();  
  73.         }  
  74.     }  
  75. }  

Welcome.xaml

  1. <Window x:Class="Login_WPF.Welcome"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  3.         Title="Welcome" Height="250" Width="400">  
  4.     <Grid>  
  5.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" x:Name="WelcomeHeading" Text="Welcome:" VerticalAlignment="Top" FontSize="17" FontStretch="ExtraCondensed"/>  
  6.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="90,10,0,0" x:Name="TextBlockName"  VerticalAlignment="Top" FontSize="15" FontStretch="ExtraCondensed" />  
  7.     </Grid>  
  8. </Window>  

At last if user login successfully then welcome message will display on another form as shown below:

registration in wpf

Figure 4