So i have a login system and when the user logins a new form opens and i want the form to display the user Name. When user register he have to put his name and surname and that 2 things i want to be displayed in the new form that opens. So is there any easy way to put the logined user name into listbox?
so here is the registration code:
MySqlConnection dataConnection = new MySqlConnection();
dataConnection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
dataConnection.Open();
MySqlTransaction transakcija = dataConnection.BeginTransaction();
MySqlCommand dataCommand = new MySqlCommand();
dataCommand.Connection = dataConnection;
dataCommand.Transaction = transakcija;
try
{
dataCommand.CommandText = "Insert INTO login.users (ime,upIme,geslo,dovoljenja) VALUES ('" + this.tB_Ime.Text + "','" + this.tB_upIme.Text + "','" + this.tB_geslo.Text + "', 'Navaden uporabnik')";
dataCommand.CommandType = CommandType.Text;
dataCommand.ExecuteNonQuery();
transakcija.Commit();
MessageBox.Show("Registracija uspešna!");
this.Hide();
}
catch (Exception eks)
{
transakcija.Rollback();
MessageBox.Show("Napaka pri registraciji\n" + eks.Message);
}
finally
{
dataCommand.Connection.Close();
}
Form1 f1 = new Form1();
f1.ShowDialog();
this.Close();
and login:
try
{
string myConnection = "datasource=localhost;port=3306;username=root;password=";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand SelectCommand = new MySqlCommand(" select * from login.users where upIme='" + this.tB_upIme.Text + "' AND geslo='" + this.tB_geslo.Text + "' ;", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
bool IsAdminUser = false;
while (myReader.Read())
{
count = count + 1;
IsAdminUser = myReader["dovoljenja"].Equals("Admin");
}
if (count == 1 && IsAdminUser == true)
{
MessageBox.Show("Prijavljeni ste kot administrator!");
this.Hide();
Form4 f4 = new Form4();
f4.ShowDialog();
}
else if (count == 1)
{
MessageBox.Show("Uspešno ste se prijavili!");
this.Hide();
Form3 f3 = new Form3();
f3.ShowDialog();
}
else if (count > 1)
{
MessageBox.Show("Dvojno uporabniško ime in geslo!");
this.Hide();
}
else
MessageBox.Show("Uporabniško ime ali geslo ni pravilno!");
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
you can send the LoggedIn UserName to the UserForm constructor and from there you can add it to the required control.
Try This:
Login Form:
string LoggedInUserName = String.Empty;
while (myReader.Read())
{
count = count + 1;
IsAdminUser = myReader["dovoljenja"].Equals("Admin");
LoggedInUserName = myReader["FirstName"].ToString()+
myReader["LastName"].ToString();
}
if (count == 1 && IsAdminUser == true)
{
MessageBox.Show("Prijavljeni ste kot administrator!");
this.Hide();
Form4 f4 = new Form4(LoggedInUserName);
f4.ShowDialog();
}
else if (count == 1)
{
MessageBox.Show("Uspešno ste se prijavili!");
this.Hide();
Form3 f3 = new Form3(LoggedInUserName);
f3.ShowDialog();
}
Now change the both admin form(Form4) and UserForm (Form3)
//now change the Admin Form Form4 constructor to take 1 argument
public Form4(string username)
{
InitializeComponent();
myListBox.Items.Add(username);//Label1.Text= username;
}
//now change the UserForm Form3 constructor to take 1 argument
public Form3(string username)
{
InitializeComponent();
myListBox.Items.Add(username);//Label1.Text= username;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.