I am using Microsoft Visual Studio 2003 and Microsoft Access.
Everytime I run this, it keeps throwing the exception "specified cast is not valid". I don't know what's wrong. Here is the code.
//variables for Employees
string strLName = "";
string strFName = "";
string strMName = "";
string strSSN = "";
//database location
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=C:\\TimeClock\\TimeClock.mdb";
OleDbConnection empConnection = new OleDbConnection(conString);
//select data
string selectStatement = "SELECT FirstName, MiddleName, LastName, SSN "
+ "FROM Employee";
OleDbCommand selectCommand = new OleDbCommand(selectStatement, empConnection);
empConnection.Open();
//activate reader
OleDbDataReader myReader = selectCommand.ExecuteReader();
//populate the list view with employees
try
{
if (myReader.HasRows)
{
while (myReader.Read())
{
strSSN = myReader.GetString(0);
strLName = myReader.GetString(1);
strFName = myReader.GetString(2);
strMName = myReader.GetString(3);
//there is a reason its checking to see if there is no SSN
//I would have to explain the whole program for you to know why
if (strSSN != "")
{
ListViewItem lvi = new ListViewItem();
lvi.Text = strSSN;
lvi.SubItems.Add(strLName);
lvi.SubItems.Add(strFName);
lvi.SubItems.Add(strMName);
lvwEmployees.Items.Add(lvi);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
myReader.Close();
empConnection.Close();