Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

ExecuteScalar gets Specified cast is not valid error

$
0
0
I just installed the VisualWebDeveloper 2005 Express Beta and am writing a test application. One problem I ran into is with a library that I wrote in the previous ASP.Net version and works well. However, I am getting an error when I run it under ASP.Net 2.0. Here's the code of the function:

public int ExecuteScalar(String sConn, String sQuery, Hashtable oParams, int nStoredProc) {
	SqlConnection oConn;
	SqlCommand oCmd;
	int nResult;


	try {
		oConn = new SqlConnection(sConn);
		oCmd = new SqlCommand(sQuery, oConn);
		if (nStoredProc == 1) {
			oCmd.CommandType = CommandType.StoredProcedure;
		}
		if (oParams != null && oParams.Count > 0) {
			foreach (DictionaryEntry oItem in oParams) {
				oCmd.Parameters.Add(new SqlParameter(oItem.Key.ToString(), oItem.Value.ToString()));
			}
		}
		oConn.Open();
		nResult = (int)oCmd.ExecuteScalar();
		oConn.Close();
		return nResult;
	} catch (SqlException e) {
		return -1;
	}
}
And here's the error that I get when this function is called: Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Line 129: } Line 130: oConn.Open();Line 131: nResult = (int)oCmd.ExecuteScalar(); Line 132: oConn.Close(); Line 133: return nResult; When I check my database, the stored procedure or query that was executed has run successfully and the changes show up in the tables. This worked without problem in pervious ASP.Net versions, but for some reason it's a problem in 2.0. If anyone has any ideas on what's wrong, I'd appreciate your assistance. Thank you.

Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>