Formularz logowania i rejestracji

0

Witam serdecznie. Od kilku dni piszę aplikację służącą do rejestracji oraz logowania korzystającą z bazy danych sql. O ile samo logowanie oraz rejestracja nie stanowiły większego problemu, "Strona Profilowa" już niestety tak. Mam problem z wyświetleniem informacji podanych przy rejestracji w oknie Profilowym zalogowanego użytkownika.

Logowanie.png
W oknie Logowania program weryfikuje wpisane ID oraz hasło i na podstawie poprawności danych okno zostaje zamknięte i otwarte okno Strony Profilowej.

strona_profilowa.png
W oknie Strony Profilowej program ma za zadanie zweryfikować podane we wcześniejszym oknie dane i na ich podstawie wyświetlić informację dotyczące profilu

Poniżej kod:

Logowanie

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Rejestracja
{
    public partial class LoginPage : Form
    {
        int id;
        public LoginPage()
        {
            InitializeComponent();
            txtPassword.PasswordChar = '*';
            txtPassword.MaxLength = 12;
        }

        private void butLogin_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "")
                MessageBox.Show("Proszę podać hasło");

            if (txtID.Text == "")
                MessageBox.Show("Proszę podać ID");

            SqlConnection sqlConnect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mnawr\OneDrive\Dokumenty\POiBD\Baza danych\Registration2.mdf;Integrated Security=True;Connect Timeout=30");
            string query = "Select * from RegistrationTable Where ID = '" + txtID.Text.Trim() + "'and Password = '" + txtPassword.Text.Trim() + "'";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sqlConnect);
            DataTable table = new DataTable();
            dataAdapter.Fill(table);

            if (table.Rows.Count == 1)
            {
            LoginPage loginPage = new LoginPage();
            this.Hide();
            UserPage userPage = new UserPage();
            userPage.ShowDialog();


            }
            else
            {
                MessageBox.Show("Błędne ID lub hasło");
            }

            

        }



        private void butBackward_Click(object sender, EventArgs e)
        {
            this.Hide();
            MainPage mainPage = new MainPage();
            mainPage.ShowDialog();
        }

        private void LoginPage_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void butExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

       
        
    }
}


Strona Profilowa

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Rejestracja
{
    public partial class UserPage : Form
    {
        
        public UserPage()
        {
            InitializeComponent();
            
        }



        private void butVerify_Click(object sender, EventArgs e)
        {
             
            
                string source = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mnawr\OneDrive\Dokumenty\POiBD\Baza danych\Registration2.mdf;Integrated Security=True;Connect Timeout=30";
                SqlConnection connection = new SqlConnection(source);
                SqlCommand cmd;
                SqlDataReader dataReader;


                String SqlSelectQuery = "Select * From RegistrationTable";
               
            try
                {
                    connection.Open();
                    MessageBox.Show("Zweryfikowano poprawnie", "Komunikat", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cmd = new SqlCommand(SqlSelectQuery, connection);
                    dataReader = cmd.ExecuteReader();

                    while (dataReader.Read())
                    {
                        txtUserID_Read.Text = dataReader["ID"].ToString();
                        txtFirstName_Read.Text = dataReader["FirstName"].ToString();
                        txtLastName_Read.Text = dataReader["LastName"].ToString();
                        txtPassword_Read.Text = dataReader["Password"].ToString();
                        txtWorkTime_Read.Text = dataReader["WorkTime"].ToString();
                        txtCountry_Read.Text = dataReader["Country"].ToString();
                        txtAge_Read.Text = dataReader["Age"].ToString();

                    }

                    connection.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            
        }

        private void butLogout_Click(object sender, EventArgs e)
        {
            this.Hide();
            MainPage mainPage = new MainPage();
            mainPage.ShowDialog();
        }

        private void UserPage_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

     
    }
}

Dodam, że aktualnie program po rejestracji i zalogowaniu wyświetla dane podane przy rejestracji, jednak nie są one zgodne z danymi podanymi przy logowaniu. Program wyświetla dane ostatnio zarejestrowanego użytkownika.

Byłbym bardzo wdzięczny za jakąkolwiek pomoc, lub wskazówkę jak odczytywać bazę danych i wyświetlać informację z niej po uprzedniej weryfikacji.

Z góry dziękuję i życzę miłego dnia

1

Może zamuliłem ale czy w tej linii

SqlSelectQuery = "Select * From RegistrationTable";

Nie powinieneś przypadkiem pobrać dane tylko dla jednego użytkownika? Czyli Select * From RegistrationTable WHERE Login LIKE "login wcześniej pobrany od użytkownika"

0

Dziękuję za szybką odpowiedź. Faktycznie powinienem odnieść się do konkretnego "textboxa". Poprawiłem kod oraz wprowadziłem metodę umożliwiającą przesłanie wartości ID z "textboxa" wykorzystywanego przy logowaniu do ukrytego textboxa w "Stronie profilowej". Następnie "textbox" na "Stronie profilowej" miał być zweryfikowany i wyświetlone powiązane z nim elementy z bazy danych. Przy próbie weryfikacji pojawił się taki oto błąd:

błąd.png

Dotyczy on ID wpisywanego w oknie "Logowanie" (3333).

Poniżej kod "Logowanie" oraz "Strona Profilowa"

"Logowanie"

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Rejestracja
{
    public partial class LoginPage : Form
    {
        
        public LoginPage()
        {
            InitializeComponent();
            txtPassword.PasswordChar = '*';
            txtPassword.MaxLength = 12;
        }

        private void butLogin_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "")
                MessageBox.Show("Proszę podać hasło");

            if (txtID.Text == "")
                MessageBox.Show("Proszę podać ID");

            SqlConnection sqlConnect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mnawr\OneDrive\Dokumenty\POiBD\Baza danych\Registration2.mdf;Integrated Security=True;Connect Timeout=30");
            string query = "Select * from RegistrationTable Where ID = '" + txtID.Text.Trim() + "'and Password = '" + txtPassword.Text.Trim() + "'";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sqlConnect);
            DataTable table = new DataTable();
            dataAdapter.Fill(table);

            if (table.Rows.Count == 1)
            {
            LoginPage loginPage = new LoginPage();
            this.Hide();
            UserPage userPage = new UserPage(this);
            userPage.ShowDialog();


            }
            else
            {
                MessageBox.Show("Błędne ID lub hasło");
            }

            

        }



        private void butBackward_Click(object sender, EventArgs e)
        {
            this.Hide();
            MainPage mainPage = new MainPage();
            mainPage.ShowDialog();
        }

        private void LoginPage_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void butExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

       
        
    }
} 

"Strona Profilowa"

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Rejestracja
{
    public partial class UserPage : Form
    {
        LoginPage lp;
        public UserPage(LoginPage lp1)
        {
            InitializeComponent();
            this.lp = lp1;
        }



        private void butVerify_Click(object sender, EventArgs e)
        {
             
            
                string source = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mnawr\OneDrive\Dokumenty\POiBD\Baza danych\Registration2.mdf;Integrated Security=True;Connect Timeout=30";
                SqlConnection connection = new SqlConnection(source);
                SqlCommand cmd;
                SqlDataReader dataReader;

                 txtID_Read.Text = lp.txtID.Text;
                 String SqlSelectQuery = "Select * From RegistrationTable where ID like'" +txtID_Read.Text;
                
            try
                {
                    connection.Open();
                    MessageBox.Show("Zweryfikowano poprawnie", "Komunikat", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cmd = new SqlCommand(SqlSelectQuery, connection);
                    dataReader = cmd.ExecuteReader();

                     

                    while (dataReader.Read())
                    {
                        txtUserID_Read.Text = dataReader["ID"].ToString();
                        txtFirstName_Read.Text = dataReader["FirstName"].ToString();
                        txtLastName_Read.Text = dataReader["LastName"].ToString();
                        txtPassword_Read.Text = dataReader["Password"].ToString();
                        txtWorkTime_Read.Text = dataReader["WorkTime"].ToString();
                        txtCountry_Read.Text = dataReader["Country"].ToString();
                        txtAge_Read.Text = dataReader["Age"].ToString();

                    }

                    connection.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            
        }

        private void butLogout_Click(object sender, EventArgs e)
        {
            this.Hide();
            MainPage mainPage = new MainPage();
            mainPage.ShowDialog();
        }

        private void UserPage_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }


    }
}

Pozdrawiam serdecznie

0

Witam. Program działa prawidłowo. Dziękuję wszystkim za udział w dyskusji i cenne wskazówki. Temat do zamknięcia.

Pozdrawiam i życzę miłego dnia.

1 użytkowników online, w tym zalogowanych: 0, gości: 1