-2

I have a program where the user has to login, so they have a id and password. The database with the user login details are stored in a local .mdf file.

I would like to set it up so that whilst the user is logged in, the rest of the program shows their details in the top right corner, so for example their name and their id.

I unfortunately have no idea how to do this and all I have seen whilst I browsed around is people using the actual System login, which is not what I want.

Code for login form:

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

namespace InventoryManager
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSubmit;
        }

        string cs = @"Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Users.mdf;Integrated Security = True;";

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtUserID.Text == "" || txtPassword.Text == "")
            {
                MessageBox.Show("Please enter a User ID and Password");
                return;
            }

            try
            {
                SqlConnection con = new SqlConnection(cs);
                SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_Login WHERE UserID = @userid AND Password = @password", con);
                cmd.Parameters.AddWithValue("@userid", txtUserID.Text);
                cmd.Parameters.AddWithValue("@password", txtPassword.Text);
                con.Open();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                int count = ds.Tables[0].Rows.Count;

                if (count == 1)
                {
                    MessageBox.Show("Login Successful!");
                    this.Hide();
                    frmOverview fo = new frmOverview();
                    fo.Show();
                }

                else
                {
                    MessageBox.Show("Login Failed");
                    txtPassword.Text = "";
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
iiAaronXiX
  • 93
  • 2
  • 3
  • 13

1 Answers1

1

You can make some static class like this one

public static class LoggedUser
{
    public static string Name { get; set; }
    public static string Username { get; set; }

}

after successful login, populate that class with data (in example):

        if (count == 1)
        {
            MessageBox.Show("Login Successful!");
            LoggedUser.Name = ds.Tables[0].Rows[1].ToString();
            LoggedUser.Username = ds.Tables[0].Rows[2].ToString();
            this.Hide();
            frmOverview fo = new frmOverview();
            fo.Show();
        }

later, you can use data stored in LoggedUser class on every form in your project...

Nino
  • 6,931
  • 2
  • 27
  • 42