Valid Login Page in WPF Application

Geetha.Katneni
Posted by Geetha.Katneni under ASP.NET category on | Points: 40 | Views : 10594
1)Go To Visual Studio 2010 -> New Project -> Select Asp.NET WPF Application -> OK
2) Design the Login Page using Xaml Code in WPF

Login.Xaml

<Window x:Class="myWpfApplication1.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="529" Title="Login">

<Grid Height="350" Width="525" >
<Grid.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#C929D4FF" Offset="0" />
<GradientStop Color="#FFE6FFFF" Offset="0.369" />
<GradientStop Color="#E387E9FF" Offset="0.934" />
</LinearGradientBrush>
</Grid.Background>


<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="LoginHeading" Text="Login:" VerticalAlignment="Top" FontSize="17" FontStretch="ExtraCondensed"/>

<TextBlock Height="50" HorizontalAlignment="Left" Margin="24,48,0,0" Name="textBlockHeading" VerticalAlignment="Top" FontSize="12" FontStyle="Italic" Padding="5">

Note: Please login here . If you are new to this site then <LineBreak /><!--line break-->

please click on


<!--textblock as a Hyperlink.-->

<TextBlock>

<Hyperlink Click="Hyperlink_Click" FontSize="14" FontStyle="Normal">Register</Hyperlink>

</TextBlock>

<!--end textblock as a hyperlink-->

button

</TextBlock>



<TextBlock Height="23" HorizontalAlignment="Left" Margin="66,120,0,0" Name="textBlock1" Text="Email" VerticalAlignment="Top" Width="67" />

<TextBlock Height="23" HorizontalAlignment="Left" Margin="58,168,0,0" Name="textBlock2" Text="Password" VerticalAlignment="Top" Width="77" />

<TextBox Height="23" HorizontalAlignment="Left" Margin="118,115,0,0" Name="TxtEmailId" VerticalAlignment="Top" Width="247" />

<PasswordBox Height="23" HorizontalAlignment="Left" Margin="118,168,0,0" Name="PwdBxPassword" VerticalAlignment="Top" Width="247" />

<Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="118,211,0,0" Name="btnLogin" VerticalAlignment="Top" Width="104" Click="btnLogin_Click" />

<TextBlock Height="23" HorizontalAlignment="Left" x:Name ="errormessage" VerticalAlignment="Top" Width="247" Margin="118,253,0,0" OpacityMask="Crimson" Foreground="#FFE5572C" />

</Grid>
</Window>

3) Add Another WPF Window in Solution Explorer with name "Welcome.aspx" to navigate on successfull login.

4)Write the following code in Login.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;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace myWpfApplication1
{
/// <summary>
/// Interaction logic for Login.xaml
/// </summary>
public partial class Login : Window
{
public Login()
{
InitializeComponent();
}

//Instantiating Welcome WPF window //
Welcome welcome = new Welcome();
private void btnLogin_Click(object sender, RoutedEventArgs e)
{


if (TxtEmailId.Text.Length == 0)

{

errormessage.Text = "Enter an Email Id";

TxtEmailId.Focus();

}
// Checking for valid Email Id Regular Expression//
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]$"))

{

errormessage.Text = "Enter a valid email.";

TxtEmailId.Select(0, TxtEmailId.Text.Length);

TxtEmailId.Focus();

}

else

{

string email = TxtEmailId.Text;

string password = PwdBxPassword.Password;

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=WPFBlog;Integrated Security=True ");

con.Open();

SqlCommand cmd = new SqlCommand("Select * from Registration where Email='" + email + "' and password='" + password + "'", con);

cmd.CommandType = CommandType.Text;

SqlDataAdapter adapter = new SqlDataAdapter();

adapter.SelectCommand = cmd;

DataSet dataSet = new DataSet();

adapter.Fill(dataSet);

if (dataSet.Tables[0].Rows.Count > 0)

{

string username = dataSet.Tables[0].Rows[0]["FirstName"].ToString() + " " + dataSet.Tables[0].Rows[0]["LastName"].ToString();

welcome.TextBlockName.Text = username; //Sending value from one form to another form.
//Navigates to Welcome Page//
welcome.Show();

Close();


}

else

{

errormessage.Text = "Sorry!! Please enter valid Email id or Password.";

}

con.Close();

}
}


}
}
}

Comments or Responses

Login to post response