I have a problem with pulling data from a SQL database.
I simply have a table with only one entry (ID) 1 (user) admin (password) 123456. When I am using SELECT * FROM users
, my Data-table contains that entry, but when I am using SELECT * FROM users WHERE user = 'admin' and password = '123456'
, my resulting Data-table is empty. Both fields are varchar(50) in the database.
This is how my code looks right now in order to be able to validate my credentials
connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=C:\USERS\SAVIC\ONEDRIVE\DOCUMENTE\USERSDATABASE.MDF;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;Authentication='Active Directory Integrated';ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
SqlDataAdapter adapter = new SqlDataAdapter("Select * from users", connection);
DataTable dt = new DataTable();
adapter.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
if(dt.Rows[i][1].ToString() == userBox.Text && dt.Rows[i][2].ToString() == pwdBox.Text)
{
this.Hide();
MainMenu main = new MainMenu(this);
main.Show();
} else
{
MessageBox.Show("Wrong username or password!", "Wrong credentials", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
}
}
I expect the output of SELECT * FROM users WHERE user = 'admin' and password = '123456'
to be the actual entry matching those conditions. All values from the database are striped of white spaces and the field names match with those used in the query.