Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

how can i create a login command in mvvm with sql database account?

In normal wpf is the login button :

C#
private void Login_Click(object sender, RoutedEventArgs e)
{
    if (txtUser.Text != "" && pwdUser.Password != "")
    {
        DBconnection objconn = new DBconnection();

        try
        {
            objconn.connection(); //calling connection

            SqlCommand com = new SqlCommand("Loginsp", objconn.con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@UserId", txtUser.Text);
            com.Parameters.AddWithValue("@password", pwdUser.Password);

            int IsValidUser = Convert.ToInt32(com.ExecuteScalar());
            if (IsValidUser == 1) //if user found it returns 1
            {
                MainWindow obj = new MainWindow();

                App.Current.MainWindow = obj;
                obj.Show(); //after login Redirect to second window
                this.Close();//after login hide the  Login window


            }
            else
            {

                MessageBox.Show("InValid UserId or Password");

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            objconn.Close();
        }
    }
    else
    {

        MessageBox.Show("UserId and Password Is Required");

    }
}


Thanks.

What I have tried:

I have tried with delegate command and Relay command but he don't execute the command.
Posted
Comments
Sinisa Hajnal 4-Apr-16 5:15am    
You create the controller which checks the database and return some result. Bind the controller action to button click.
VR Karthikeyan 4-Apr-16 6:02am    
You are showing MainWindow after login. So where you placed the login button?
DarkArT69 4-Apr-16 12:47pm    
This code what i post was the normal code and i want to implement in mvvm or transform it in command
Richard Deeming 4-Apr-16 8:56am    
Don't store passwords in plain text. Instead, store a salted hash of the password, using a unique salt per record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
DarkArT69 4-Apr-16 12:47pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900