I have a login screen and I need to pass username to my main form (for getting permissions etc.). Here is my code:
//Login
private void button1_Click(object send
This is the best way to transfer data from one form to another, On the LoginForm.cs write like this:
ex.UserName = txtUserName.text;
Password=txtPassword.text;
MainForm mainForm = new MainForm(UserName,Password);
this.Hide();
mainForm.Show();
In the MainForm.cs edit the
public MainForm () { }
like this:
public MainForm(string userName,string password){
}
In login form
public string UserName {get; private set;}
if (string.Compare(dbPassword, appPassword) == 0)
{
UserName = txtUser.Text;
//I need to pass username value to myForm...
DialogResult = DialogResult.OK;
}
else
{
//Show warning
}
in main
DialogResult result;
using (var loginForm = new Login())
result = loginForm.ShowDialog();
if (result == DialogResult.OK)
{
var username = loginForm.UserName;
Application.Run(new myForm(username));
}
Expose username as a string property of your login form class. This way you'll be able to fetch it after the form will be closed (it will still remain in memory).
it is simply use EF on your codes just like below
}
Siman_dbEntities db = new Siman_dbEntities();
public string UserNameLogedIn;
private void btnLogin_Click(object sender, EventArgs e)
{
var login = from b in db.Tbl_Users.Where(b => b.Username == txtUsername.Text && b.Password == txt_Password.Text)
select b;
if (login.Count()==1)
{
this.Hide();
main frmmain = new main();
frmmain.Show();
}
var query = db.Tbl_Users.Where(c => c.Username == txtUsername.Text).Single();
UserNameLogedIn = query.Name.ToString();
}