- Back to Home »
- WPF »
- WPF Part-5 Using ADO.NET for register and login operations
Posted by :
Sudhir Chekuri
Thursday, 3 October 2013
WPF application with register and signin using ADO.NET
In am creating a WPF application that contains three windows Login, Home and Register.I will create a MS SQL Server database table for registered user details.
I will use background image for signin window designing.
Login window is launched first, With successful login he will enter into home window.
Register link in login window will navigate him to Register window for registration.
XAML, C#.NET and ADO.NET is used in this application.
All ADO.NET classes and methods can be used in WPF same like Windows applicatons.
[images of the output app]
Create Database table using the below SQL queries in SQL Server Management Studio
--creating database
create database DB_WPF
use DB_WPF
--creating table for registration details
create table tbl_register(sno int identity,username varchar(30),pwd varchar(30),emailid varchar(50))
select * from tbl_register
--inserting test data into registration table
insert into tbl_register(username,pwd,emailid) values('sudhir','sudhir','s@gmail.com')
Creating WPF application
Open visual studio and create new WPF project named as WPF_ADO
Create three windows MainWindow.xaml, Home.xaml, Register.xaml
Lets start with Login window(MainWindow.xaml)
Change the Title,height,width in xaml code.
Title="Login" Height="350" Width="600"
Copy and paste the above image into your solution explorer in Images folder to use it in your project.
Steps to create login form using GUI:
Select grid go to its properties - brush - background - click on image icon(4th from left side) - Dropdown image source - Images/LoginBackground.jpg
Drag and drop one textbox beside username and passwordbox beside password.
Modify properties like name, text="" and adjust the locations as shown in the below xaml code.
<TextBox x:Name="TxtUsername" HorizontalAlignment="Left" Height="23" Margin="266.455,102.168,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="90.629"/>
<PasswordBox x:Name="TxtPassword" HorizontalAlignment="Left" Height="23" Margin="266.455,156.239,0,0" VerticalAlignment="Top" Width="90.629"/>
textblock with mouseup event is used as login button.
<TextBlock x:Name="TBlockSignIn" HorizontalAlignment="Left" Margin="373.798,130.339,0,0" FontSize="24" Text="Sign In" VerticalAlignment="Top" TextAlignment="Center" Height="65.042" MouseUp="TBlockSignIn_MouseUp" />
For link to register page i used textblock with mouseup event
<TextBlock x:Name="TBlockRegister" HorizontalAlignment="Left" Margin="388.831,215.306,0,0" TextWrapping="Wrap" Text="Register" VerticalAlignment="Top" TextDecorations="Underline" MouseUp="TBlockRegister_MouseUp"/>
XAML code for MainWindow.xaml
<Window x:Class="WpfApp_ADO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Login" Height="350" Width="600">
<Grid >
<Grid.Background>
<ImageBrush ImageSource="Images/LoginBackgroundNew.png"/>
</Grid.Background>
<TextBox x:Name="TxtUsername" HorizontalAlignment="Left" Height="23" Margin="266.455,102.168,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="90.629"/>
<PasswordBox x:Name="TxtPassword" HorizontalAlignment="Left" Height="23" Margin="266.455,156.239,0,0" VerticalAlignment="Top" Width="90.629"/>
<TextBlock x:Name="TBlockRegister" HorizontalAlignment="Left" Margin="388.831,215.306,0,0" TextWrapping="Wrap" Text="Register" VerticalAlignment="Top" TextDecorations="Underline" MouseUp="TBlockRegister_MouseUp"/>
<TextBlock x:Name="TBlockSignIn" HorizontalAlignment="Left" Margin="373.798,130.339,0,0" FontSize="24" Text="Sign In" VerticalAlignment="Top" TextAlignment="Center" Height="65.042" MouseUp="TBlockSignIn_MouseUp" />
</Grid>
</Window>
Note: Textbox - Text, Button - Content, PasswordBox - Password
C#.NET Code in MainWindow.xaml code to check login and navigate to Home page:
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.SqlClient;
using System.Data;
namespace WpfApp_ADO
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Data Source=SUDEER;Initial Catalog=DB_WPF;Integrated Security=True");
private void TBlockRegister_MouseUp(object sender, MouseButtonEventArgs e)
{
Register r = new Register();
r.Show();
this.Hide();
}
private void lblSignin_MouseUp(object sender, MouseButtonEventArgs e)
{
Home h = new Home(TxtUsername.Text);
h.Show();
this.Hide();
}
private void TBlockSignIn_MouseUp(object sender, MouseButtonEventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from tbl_register where username='" + TxtUsername.Text + "' and pwd='" + TxtPassword.Password + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
//sending username to home window to display welcome msg
Home h = new Home(TxtUsername.Text);
h.Show();
this.Hide();
}
else { MessageBox.Show("Invalid credentials"); }
}
}
}
XAML code for Home.xaml
<Window x:Class="WpfApp_ADO.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Home" Height="300" Width="300">
<Grid>
<Label x:Name="LblMsg" />
</Grid>
</Window>
C#.NET Code for Home.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.Shapes;
namespace WpfApp_ADO
{
/// <summary>
/// Interaction logic for Home.xaml
/// </summary>
public partial class Home : Window
{
public Home(string s)
{
InitializeComponent();
LblMsg.Content ="Welcome "+ s;
}
}
}
XAML code for Register.xaml
<Window x:Class="WpfApp_ADO.Register"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Register" Height="300" Width="300">
<Grid Margin="5">
<Grid.RowDefinitions >
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Username:" Grid.Row="0" Grid.Column="0"></TextBlock>
<TextBlock Text="Password:" Grid.Row="1" Grid.Column="0"></TextBlock>
<TextBlock Text="Confirm Password:" Grid.Row="2" Grid.Column="0"></TextBlock>
<TextBlock Text="Emailid:" Grid.Row="3" Grid.Column="0"></TextBlock>
<TextBox x:Name="TxtUsername" Grid.Row="0" Grid.Column="1" Height="30" VerticalAlignment="Top" Width="130" HorizontalAlignment="Left" ></TextBox>
<PasswordBox x:Name="TxtPwd" Grid.Row="1" Grid.Column="1" Height="30" VerticalAlignment="Top" Width="130" HorizontalAlignment="Left" ></PasswordBox>
<PasswordBox x:Name="TxtCfPwd" Grid.Row="2" Grid.Column="1" Height="30" VerticalAlignment="Top" Width="130" HorizontalAlignment="Left" ></PasswordBox>
<TextBox x:Name="TxtEmailid" Grid.Row="3" Grid.Column="1" Height="30" VerticalAlignment="Top" Width="130" HorizontalAlignment="Left" ></TextBox>
<Button x:Name="BtnRegister" Grid.Row="4" Content="Register" Grid.Column="1" Height="30" VerticalAlignment="Top" Width="130" HorizontalAlignment="Left" Click="BtnRegister_Click" ></Button>
<TextBlock x:Name="TBlockRegister" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Login" VerticalAlignment="Top" TextDecorations="Underline" MouseUp="TBlockRegister_MouseUp"/>
</Grid>
</Window>
C#.NET code for Register.xaml.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
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.Shapes;
namespace WpfApp_ADO
{
/// <summary>
/// Interaction logic for Register.xaml
/// </summary>
public partial class Register : Window
{
public Register()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Data Source=SUDEER;Initial Catalog=DB_WPF;Integrated Security=True");
private void BtnRegister_Click(object sender, RoutedEventArgs e)
{
if (TxtPwd.Password == TxtCfPwd.Password )
{
SqlCommand cmd = new SqlCommand("insert into tbl_register(username,pwd,emailid) values('" + TxtUsername.Text + "','" + TxtPwd.Password + "','" + TxtEmailid.Text + "')", con);
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Successfully registered");
}
else { MessageBox.Show("Registration failed"); }
}
else { MessageBox.Show("Password Mismatch"); }
}
private void TBlockRegister_MouseUp(object sender, MouseButtonEventArgs e)
{
MainWindow m = new MainWindow();
m.Show();
this.Hide();
}
}
}