JayJay
JayJay

Reputation: 1068

Binding TextBlock Linq ToSql & WPF

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?

Upvotes: 0

Views: 507

Answers (3)

jEROD
jEROD

Reputation: 51

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);
}

Upvotes: 0

Rami Olaqi
Rami Olaqi

Reputation:

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();

    }

Upvotes: 1

Ayman
Ayman

Reputation: 20

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
}

Upvotes: 0

Related Questions