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=Micro soft.Jet.OLEDB. 4.0;"
+ "Data Source=C:\\Time Clock\\TimeCloc k.mdb";
OleDbConnection empConnection = new OleDbConnection (conString);
//select data
string selectStatement = "SELECT FirstName, MiddleName, LastName, SSN "
+ "FROM Employee";
OleDbCommand selectCommand = new OleDbCommand(se lectStatement, empConnection);
empConnection.O pen();
//activate reader
OleDbDataReader myReader = selectCommand.E xecuteReader();
//populate the list view with employees
try
{
if (myReader.HasRo ws)
{
while (myReader.Read( ))
{
strSSN = myReader.GetStr ing(0);
strLName = myReader.GetStr ing(1);
strFName = myReader.GetStr ing(2);
strMName = myReader.GetStr ing(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.Ad d(strLName);
lvi.SubItems.Ad d(strFName);
lvi.SubItems.Ad d(strMName);
lvwEmployees.It ems.Add(lvi);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
myReader.Close( );
empConnection.C lose();
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=Micro soft.Jet.OLEDB. 4.0;"
+ "Data Source=C:\\Time Clock\\TimeCloc k.mdb";
OleDbConnection empConnection = new OleDbConnection (conString);
//select data
string selectStatement = "SELECT FirstName, MiddleName, LastName, SSN "
+ "FROM Employee";
OleDbCommand selectCommand = new OleDbCommand(se lectStatement, empConnection);
empConnection.O pen();
//activate reader
OleDbDataReader myReader = selectCommand.E xecuteReader();
//populate the list view with employees
try
{
if (myReader.HasRo ws)
{
while (myReader.Read( ))
{
strSSN = myReader.GetStr ing(0);
strLName = myReader.GetStr ing(1);
strFName = myReader.GetStr ing(2);
strMName = myReader.GetStr ing(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.Ad d(strLName);
lvi.SubItems.Ad d(strFName);
lvi.SubItems.Ad d(strMName);
lvwEmployees.It ems.Add(lvi);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
myReader.Close( );
empConnection.C lose();
Comment