Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Autonumber field and OleDbDataReader

Status
Not open for further replies.

brd24gor

MIS
May 18, 2005
123
US
I keep getting an invalid cast exception when trying to retrieve data from an AutoNumber field in Access. Here is what I have:
Code:
		private int GetSessionNumber()
		{
			//Check database connection.  If closed, open it
			if (aLib.SQLConnection.State==System.Data.ConnectionState.Closed)
				aLib.SQLConnection.Open();

			string sql = "SELECT MAX(Session_ID) As S FROM User_Tests " + 
				"WHERE Test_ID = '" + testID + "' AND User_ID = '" + taker.UserID + "'";
			OleDbCommand getSession = new OleDbCommand(sql,aLib.SQLConnection);
			OleDbDataReader sessionReader = getSession.ExecuteReader();
			sessionReader.Read();
			int session = sessionReader.GetInt32(0); //Invalid Cast
			MessageBox.Show(Convert.ToString(session));
			sessionReader.Close();
			return(session);
		}

My problem is occuring on this line:
Code:
sessionReader.GetInt32(0); //Invalid Cast

I have the AutoNumber column setup as a Long Integer in Access. Is there something about the AutoNumber type that I need to know to be able to use a DataReader on it?

Thanks,
Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top