views:

225

answers:

3

Hi, i have a LoginWindow with username and password to access in the software after that the user authenticated i want show in the next window(the mainWindow of the software) the name of the user authenticated in a TextBlock ...i show a code snippet of my LoginWindow:

public partial class Window1 : Window { public Window1() { InitializeComponent(); }

    public bool ValidateApplicationUser(string userName, string password)
    {
      {
            var AuthContext = new DataClasses1DataContext();
            var query = from c in  AuthContext.Users
                        where (c.Username == userName.ToLower() && c.Password == password.ToLower())
                        select c;

            if(query.Count() != 0 )
            {
                return true;
            }

            return false;
        }

    }

    private void mahhh(object sender, RoutedEventArgs e)
    {
        bool authenticated = true;
        {
            if (usernameTextBox.Text !="" && passwordTextBox.Text != "")
            {
                authenticated = ValidateApplicationUser(usernameTextBox.Text , passwordTextBox.Text);
            }

        }
        if (!authenticated)
        {
            MessageBox.Show("Invalid login. Try again.");
        }
        else
        {
            MessageBox.Show("Congradulations! You're a valid user!");
            MainWindow c = new MainWindow();
            c.ShowDialog();

        }
    }
}

If i authenticate with the username"Marc" in the MainWindow i will show the username "Marc" in a TextBlock and i don't know i make it? How i can do it?

Thanks and Happy Day.

A: 

Simply, Pass the UserName to the constructor of the Main window like this

MainWindow c = New MainWindow(usernameTextBox.Text);

And in the constructor of the main window receive the value in variable and do whatever you want with it, like this

private String _userName;

public MainWindow(string userName)
{ 
    _userName = userName
}
Ayman
+1  A: 

i think you had some mistake in yr code (it will allow empty field to log) , it has to be like :

 bool authenticated = true;
    {
        if (usernameTextBox.Text !="" && passwordTextBox.Text != "")
        {
            authenticated = ValidateApplicationUser(usernameTextBox.Text , passwordTextBox.Text);
        }


    }
    if (!authenticated || usernameTextBox.Text == "" || passwordTextBox.Text == "")
    {
        MessageBox.Show("Invalid login. Try again.");
    }
    else
    {
        MessageBox.Show("Congradulations! You're a valid user!");
        MainWindow c = new MainWindow();
        c.ShowDialog();

    }
A: 

add a public variable/property to your MainWindow Class

public string Username { get; set; }

now you can set the property

MessageBox.Show("Congradulations! You're a valid user!");
MainWindow c = new MainWindow();
c.Username = usernameTextBox.Text;
c.ShowDialog();

and use it in your MainWindow

MainWindow_Loaded(..) {      
   MessageBox.Show("You are " + Username);
}
jEROD