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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalid row handle

Status
Not open for further replies.

brd24gor

MIS
May 18, 2005
123
US
I am trying to to retrieve a value from a table called Session_ID. When the line in my try block is executed, I get a "Row handle is invalid" error. The value I am trying to get is a long integer in an Access database. I have also tried a SELECT MAX and it still doesn't work. You guys have any helpful suggestions?

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 Session_ID FROM User_Tests " + 
				"WHERE Test_ID = '" + testID + "' AND User_ID = '" + taker.UserID + "' " +
				"ORDER BY Session_ID DESC";
			OleDbCommand getSession = new OleDbCommand(sql,aLib.SQLConnection);
			OleDbDataReader sessionReader = getSession.ExecuteReader();
			sessionReader.Read();
			//MessageBox.Show(Convert.ToString((int)sessionReader.GetValue(0)));
			try
			{
				int sessionID = sessionReader.GetInt32(0); 
			} 
			catch(Exception e) { MessageBox.Show(e.ToString()); }
			sessionReader.Close();
			//return(sessionID);
			return 0;
		}
 
Is Session_ID an integer? Maybe you should try using sessionReader.GetValue(0) instead of sessionReader.GetInt32(0);
 
Yeah, it is defined as a long integer field in the database. I tried the GetValue method in the commented out MessageBox.Show line and still had an error. I am trying to use the ExecuteScalar() and SQL SELECT MAX right now, but I keep getting a "Specified cast is not valid" error on my ExecuteScalar() statement:

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 SNum FROM User_Tests " + 
				"WHERE Test_ID = '" + testID + "' AND User_ID = '" + taker.UserID + "'";
				
			OleDbCommand getSession = new OleDbCommand(sql,aLib.SQLConnection);

			try
			{

				int sessionID = (int)getSession.ExecuteScalar();
				MessageBox.Show(Convert.ToString(sessionID));
			} 
			catch(Exception e) { MessageBox.Show(e.ToString()); }

			return 0;
		}

I can't seem to figure out how to cast a MAX statement. I have tried strings and all of the ints and have not had any luck. Any suggestions at this point are greatly apprecited. I have been banging my head on the table on this since last week.

--Brad
 
Also change
e.ToString()

to e.description ,source,innerexception etc
 
I am still getting "Specified cast is not valid" errors for Int16, Int32, and Int64 values. I may just delete and re-create the table to see if the definition is somehow corrupted. I am completely out of ideas if that doesn't work.
Any other ideas?

--Brad
 
Wow. All I can say is I am a complete idiot. The problem ended up being with one of the variables in my SQL statement. It is pretty hard to cast a value from a query that doesn't return anything :)

I apologize for taking up valuable time for anyone who tried to help. Thanks again!

--Brad
 
why do you convert it at the start?

try
string sTest = getSession.ExecuteScalar().ToString();
MessageBox.Show(sTest);

and see what it gives you,cant you debug this using breakpoint and quickwatch?
 
The MessageBox after the ExecuteScalar() call was for debugging purposes and will not be part of the program. This is why I casted as an int and then converted it to a string for the MessageBox. The method will eventually return sessionID.

--Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top