标签云

微信群

扫码加入我们

WeChat QR Code

I have a problem with creating an active login panel. To the point ! I want to create a login panel where the client has to log into the system, below I present to you the forms I have.

  1. If the client provides PESEL and password and exists in the database, he can log in.

2. If the client provides a PESEL which is incorrect, then he / she will have to pop out the window with the information "PESEL invalid or does not exist".

  1. And here the problem starts with me ... I want to do something like that if a customer who has registered before and added to the database, the window with the message "You have been registered" has already popped up - now enter only the PESEL in the login panel and then create the password. "

After going to the login panel, the client is required to enter only the correct PESEL for the first time, the login form checks if the given PESEL exists and has a password assigned, if not, open the form window with the new password and repeat it. This password is to be added to the database where the PESEL is previously provided.

My work:

  • for the login panel:

enter image description here

code for login panel:

using System.Linq;
using System.Windows;
using eDoctor.Functions;

namespace eDoctor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    eDoctorEntities entities = new eDoctorEntities();
    Password password = new Password();
    Pacjent pacjent;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string PeselTextBox = Pesel.Text;
        string PassCheck = Passwd.Password;


        if (!string.IsNullOrWhiteSpace(PeselTextBox))
        {
            if (!PeselTextBox.VPesel())
            {
                MessageBox.Show("PESEL nieprawidłowy lub nie istnieje");
            }

            if (string.IsNullOrEmpty(PassCheck))
            {
                if (entities.Pacjent.Any(x => x.Pesel == PeselTextBox))
                {
                    Password pass = new Password();

                    pass.passwd1.Text = "";
                    pass.Show();

                    pacjent.Pesel = password.CheckPasswd(PeselTextBox);
                }
            }
            else
            {
                MessageBox.Show("Hasło nieprawidłowe");
            }
        }
        else if (string.IsNullOrEmpty(PeselTextBox))
        {
            MessageBox.Show("Pole PESEL jest pusty, proszę uzupełnić");
        }
        else
        {
            MessageBox.Show("PESEL jest pusty, proszę uzupełnić dane");
        }
    }

    private void Button_Click_Register(object sender, RoutedEventArgs e)
    {
        Register register = new Register();
        register.Show();
        }
    }
}

For panel new password:

enter image description here

code:

using System;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace eDoctor
{
/// <summary>
/// Interaction logic for Password.xaml
/// </summary>
public partial class Password : Window
{
    Pacjent model = new Pacjent();
    public TextBox PasswordBox;
    string pswd;

    public Password()
    {
        InitializeComponent();
        PasswordBox = passwd1;
    }

    private void SendPassw_Click(object sender, RoutedEventArgs e)
    {
        CheckPasswd(pswd);
    }

    static string GetMD5Hash(string input)
    {
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            byte[] data = md5.ComputeHash(utf8.GetBytes(input));
            return Convert.ToBase64String(data);
        }
    }

    public string CheckPasswd(string pswd)
    {
        if (passwd1.Text == passwd2.Text)
        {
            if (string.IsNullOrEmpty(passwd1.Text) || string.IsNullOrEmpty(passwd2.Text))
            {
                MessageBox.Show("The field can not be empty!);
            }
            var result = GetMD5Hash(passwd2.Text);

            var query = $"UPDATE Pacjent SET ShashowaneHaslo = '{result}' WHERE Pesel='{pswd}'";
            using (eDoctorEntities db = new eDoctorEntities())
            {
                db.Database.ExecuteSqlCommand(query);
            }
        }
        else
        {
            MessageBox.Show("The given passwords are not the same, enter the password again!");
        }
        return pswd;
        }
    }
}

Model database:

enter image description here

I did not give the registration because it works and you can add records in the registration form, only unfortunately there is a problem to update the data and add a password to the database, where is the PESEL in which it was given during login. How to do it ? Maybe some other better solution?

From debug:

I run the program and I try to write PESEL in login, I click Enter, I create to create a new password, but immediately I get the message that 'field can not be empty', I press 'OK' and error..

Error program:

System.NullReferenceException: 'The object reference has not been set to the instance of the object.' pacjent was null.

Plase help me.


At which exact line of code does it throw this error?

2018年06月20日28分41秒

such a stupid mistake and so much searching, my inattention. Thank you ! Programmer's beer for you user1672994 :D

2018年06月19日28分41秒