I'm developing an application that reads the users data from a database (MS Access) and sets the information into variables. The sql command takes a parameter to check the username against the database, however the OleDbDataReader is showing up empty (userData.HasRows).
public void UpdatePage(string Username)
{
OleDbDataReader userData;
string sqlCmd = "SELECT * FROM [Profile Data] WHERE Username = ?";
TabPage profile = new TabPage();
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = LogIn Profiles.accdb;"))
{
OleDbCommand cmd = new OleDbCommand(sqlCmd, conn);
cmd.Parameters.AddWithValue("@Username", Username);
try
{
conn.Open();
userData = cmd.ExecuteReader();
if(userData.HasRows)
{
//Used for debugging
}
while (userData.Read()) //is not entering into the loop
{
userID = userData[0].ToString();
username = userData[1].ToString();
password = userData[2].ToString();
country = userData[3].ToString();
occupation = userData[4].ToString();
gender = userData[5].ToString();
}
userData.Close();
}
catch
{
MessageBox.Show("Error 3: Error to connect to database", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
When I remove the parameter and set the sqlCmd to have a set name ("...WHERE Username = 'Mitchell'";) in the database, then the while loop is entered and the variables are assigned. Awesome. So something is happening with the parameter.
Also I have confirmed that the user data is in the database before this method is called, so it should be finding it.