d4ne d4ne - 4 months ago 19
C# Question

C# after login, redirect to another form with existing parameters

I'm trying to redirect a authenticated user to another c# form.

After the user is authenticated using his username and password he will be sent to another form. Sadly I can't access the old parameters from the old form element.

Here's a screenshot of both form elements: enter image description here

My code looks like the following:

Form 1:

using MaterialSkin;
using MaterialSkin.Controls;
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;

namespace Helpful
{
public partial class Form1 : MaterialForm
{
public Form1()
{
InitializeComponent();

var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}

private void materialRaisedButton1_Click(object sender, EventArgs e)
{
try
{
string username = materialSingleLineTextField1.Text;
string password = materialSingleLineTextField2.Text;
database_connector dbConnect = new database_connector();
bool db_response = dbConnect.user_check(username, password);

if (db_response == true)
{
MessageBox.Show("User authentificated.");
new Form2().Show();
this.Hide();
}
else
{
MessageBox.Show("Please try again, wrong user credentials.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}


Form 2:

using MaterialSkin;
using MaterialSkin.Controls;
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;

namespace Helpful
{
public partial class Form2 : MaterialForm
{
public Form2()
{
InitializeComponent();

var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}
}
}


The database_connector class:

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 MySql.Data.MySqlClient;

namespace Helpful
{
class database_connector
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;

// Constructor
public database_connector()
{
Initialize();
}

//Initialize values
private void Initialize()
{
server = "xxx";
database = "xxx";
uid = "xxx";
password = "xxx";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

connection = new MySqlConnection(connectionString);
}

//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator.");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}

//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}

// Select statement
public bool user_check(string username, string password)
{
string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";

bool hasRecords = false;

if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
if (dataReader.HasRows)
{
while (dataReader.Read())
{
hasRecords = true;
}
}
dataReader.Close();
this.CloseConnection();
}
return hasRecords;
}
}
}





My question is, how could I use the variable username in the form 2 now without the user entering it again?

I would appreciate any kind of help.

Answer

To achieve what you want, following the POO and Layers programming, you must create an class which will save your data.

With the following example I made, you could in the first screen set the parameters and it would be accessible from any other form which can see this class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class User
    {
        static private string cdUser;
        static private int cdAcess;
        static private string nmUser;

        public static string CdUser
        {
            get
            {
                return cdUser;
            }

            set
            {
                cdUser = value;
            }
        }
        public static int CdAcess
        {
            get
            {
                return cdAcess;
            }

            set
            {
                cdAcess = value;
            }
        }
        public static string NmUser
        {
            get
            {
                return nmUser;
            }

            set
            {
                nmUser = value;
            }
        }
    }
}

To save your data, you could do something like that:

User.CdUser = _login;
User.CdAcess = Convert.ToInt32(rdr["acess"].ToString());
User.NmUser = rdr["name"].ToString();
Comments