1

I am trying to figure out how to create a basic login system using UWP and MSSQL but I can't seem to figure it out. I used the Microsoft website to find the information on how to connect a database to UWP but the problem is when I want to test the login condition. NOTE: This is just for me trying to figure it out. I am still a student.

public MainPage()
    {
        this.InitializeComponent();

    }

    public string ConnectionString { get; set; } = @"Data Source =.; Initial Catalog = LoginTest; Integrated Security = True";

    private void Button_Click(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

    }
    public ObservableCollection<UsersLogin> GetProducts(string connectionString)
    {
        const string GetProductsQuery = "select username, password, from Products";

        var products = new ObservableCollection<UsersLogin>();
        try
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = GetProductsQuery;
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                if (username == UsernameText.Text() && password == PasswordText.Text())
                                {
                                    Frame.Navigate(typeof(page));
                                }
                            }
                        }
                    }
                }
            }
            return products;
        }
        catch(Exception ex)
        {

        }
    }
}

Here is my class:

public class UsersLogin: INotifyPropertyChanged
{
    public string username { get; set; }
    public string password { get; set; }


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

The problem is, that it cant find the username or password properties from the class. Can someone help?

Dozy
  • 11
  • 5
  • Possible duplicate of [How to retrieve data from a SQL Server database in C#?](https://stackoverflow.com/questions/14171794/how-to-retrieve-data-from-a-sql-server-database-in-c) – obl Nov 05 '18 at 16:08

1 Answers1

3

I think you need to get the values from the reader object

while (reader.Read())
{
    if (reader[0].ToString() == UsernameText.Text() && reader[1].ToString() == PasswordText.Text())
    {
        Frame.Navigate(typeof(page));
    }
}

Or you can read the values into username / password first, and then compare as you did, like so:

UsersLogin userLogin = new UsersLogin();
userLogin.username = reader[0].ToString();
userLogin.password = reader[1].ToString();

Also you may want to look at this example of using INotifyPropertyChanged

obl
  • 1,799
  • 12
  • 38
  • How will I read In the values? sorry for asking.First time doing a login page with UWP – Dozy Nov 05 '18 at 16:08