I have a C# desktop application project for school that uses an Access 2003 database (not designed by me) and I am running into issues using OleDBDataReader. It has been awhile since I have programmed anything and I have never had to connect to an Access database before, so this is a little new. What I am trying to do is create a login screen based off of the employee table in the database. There is no password yet, I am just trying to work the code out to even search the database for the user name (F_Name in the table). The form has a username text box, a password text box and a login command button.
The problem that I run in to is when I launch the app and type anything into Username and click the login button, the debugger highlights line
reader = objCommand.ExecuteReader();
and says "No value given for one or more required parameters."
I am using Visual Studio 2008.
This is the code that have thus far.
private void btnLogin_Click(object sender, EventArgs e)
{
string sqlEmployeeSearch = "Select F_Name from tblEmployee WHERE F_Name = "+txtUsername.Text+"";
string dbconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\WSC_teama.mdb;User Id=admin;Password=";
OleDbConnection objConnection = new OleDbConnection(dbconnect);
OleDbCommand objCommand = new OleDbCommand(sqlEmployeeSearch, objConnection);
OleDbDataReader reader = null;
searchTerm = txtUsername.Text;
objConnection.Open();
reader = objCommand.ExecuteReader();
try
{
while (reader.Read())
//Info += reader.GetString(0).ToString() + reader.GetString(1) + reader.GetString(2).ToString() + "\n";
//MessageBox.Show(reader.GetValue(int).ToString());
if (searchTerm == reader.GetString(0))
{
MessageBox.Show("Winner!");
empFName = reader.GetString(0).ToString();
}
}//closes try
catch (Exception ex)
{
MessageBox.Show("read meathod " + ex);
}//closes catch
reader.Close();
objConnection.Close();
If there is a better way to do this, I am not above using different methods to achieve the same goal.
Any help is appreciated. Thanks in advance!