Hello,
I have my method which checks if the user is in the database or not upon login.I would like this to be done by using a progress bar,so every time the progress bar reaches 100,during the loading process,the method will check the credentials with the database and automatically will load the window if the credentials are correct,without involving any button at all.Can someone please tell me if this s this possible?And if yes,how should I do it?I have searched examples over the internet,but there is nothing that can be found,only basic explanations about how to make it work and some basic functionalities of it.I would really appreciate an advice,and hope someone will make a topic about it in time,since it's useful to learn when you have a project where you have to avoid buttons and see how can you establish a relation between the database and the progress bar.I'm using WPF with MVVM,my strategy was database first.Thank you in advance!
What I have tried:
I have tried this:
class LoginWithCardreaderViewModel : INotifyPropertyChanged
{
private readonly BackgroundWorker worker;
private readonly DelegateCommand instigateWorkCommand;
private int currentProgress;
private bool isDataFetched;
public bool IsDataFetched
{
get { return isDataFetched; }
set
{
if(isDataFetched=value)
{
NotifyOnPropertyChange("IsDataFetched");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyOnPropertyChange(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private DelegateCommand loginCommand;
private int pwd;
public int PWD
{
get { return pwd; }
set
{
if (pwd != value)
{
pwd = value;
NotifyOnPropertyChange("PWD");
}
}
}
public LoginWithCardreaderViewModel()
{
this.instigateWorkCommand = new DelegateCommand(o => this.worker.RunWorkerAsync(), o => !this.worker.IsBusy);
this.worker = new BackgroundWorker();
this.worker.DoWork += this.DoWork;
this.worker.ProgressChanged += this.ProgressChanged;
}
private void ProgressChanged(object sender,ProgressChangedEventArgs e)
{
this.currentProgress = e.ProgressPercentage;
}
public int CurrentProgress
{
get { return currentProgress; }
set
{
currentProgress = value;
NotifyOnPropertyChange("CurrentProgress");
}
}
private void DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\VIAApp2Demo\VIAApp2Demo\DB\DatabaseStudents.mdf;Integrated Security=True;Connect Timeout=30");
try
{
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
String query = "SELECT COUNT (*) FROM Login WHERE pwd=@pwd";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandType = CommandType.Text;
SqlParameter Pwd = cmd.Parameters.AddWithValue("@pwd", SqlDbType.Int);
cmd.Parameters["@pwd"].Value = pwd;
if (Pwd.Value == null)
{
Pwd.Value = DBNull.Value;
}
SqlDataReader reader = cmd.ExecuteReader();
Thread.Sleep(300);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (currentProgress == 100 && count > 0)
{
MainWindow main = new MainWindow();
main.Show();
}
else
{
MessageBox.Show("Username or password is incorrect!");
Thread.Sleep(500);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
This is the binding with the view(.cs):
public partial class LoginWithCardreader : Window
{
public LoginWithCardreader()
{
InitializeComponent();
LoginWithCardreaderViewModel vm = new LoginWithCardreaderViewModel();
this.DataContext = vm;
}
}
And this is the .xaml part:
<Window x:Class="LastAndFinalVersion.View.LoginWithCardreader"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LastAndFinalVersion.Helpers"
xmlns:mm="clr-namespace:LastAndFinalVersion.ViewModel"
mc:Ignorable="d"
<Window.DataContext>
<mm:LoginWithCardreaderViewModel/>
</Window.DataContext>
<pre> Title="LoginWithCardreader" Height="400" Width="400">
<Grid>
<Border Background="#2e3137" CornerRadius="20" Margin="20">
<StackPanel Margin="20">
<Label Content="Login"
Foreground="White" FontSize="25" HorizontalAlignment="Center"/>
<Separator></Separator>
<TextBlock Height="50"
HorizontalAlignment="Center"
Margin="24,48,0,0"
Name="textBlockHeading"
VerticalAlignment="Top"
FontSize="12"
Foreground="Crimson"
FontStyle="Italic"
Padding="5">
Please swipe the card on the<LineBreak></LineBreak> card reader for authentication!
</TextBlock>
<ProgressBar x:Name="loginProgress" Height="10" Margin="0,0,-0.4,0" />
<Label Content="Password" Foreground="White"/>
<PasswordBox x:Name="passwordBox"
MaxLength="10"
local:PasswordHelper.BindPassword="true"
local:PasswordHelper.BoundPassword="{Binding Path=PWD, Mode=TwoWay, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"
PasswordChar="*"
Background="#545d6a"
Foreground="White"
FontSize="18"/>
</StackPanel>
</Border>
</Grid>
</Window>