What's wrong with this DataReader????

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themoonisdown09
    New Member
    • Jul 2006
    • 11

    What's wrong with this DataReader????

    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();
  • themoonisdown09
    New Member
    • Jul 2006
    • 11

    #2
    I failed to mention that it's in C#.

    Comment

    • dna5122
      New Member
      • Jul 2006
      • 12

      #3
      You also failed to mention which line throws the exception. That would help alot.

      Comment

      • themoonisdown09
        New Member
        • Jul 2006
        • 11

        #4
        I figured it out now. It's all good.

        Comment

        Working...