I’m getting “System.InvalidCastException: Specified cast is not valid.”

With the line "cmdThisIDTest = ((int)commandThisID.ExecuteScalar());" in the code below I'm getting "System.InvalidCastException: Specified cast is not valid." How do I fix this?

Thanks.

using (var conn = new SqlCeConnection(dbConnectionString)) { conn.Open(); var commandThisID = new SqlCeCommand("SELECT MAX(thisID) FROM table1"); commandThisID.Connection = conn; object ThisIDTest = commandThisID.ExecuteScalar(); int cmdThisIDTest = 0; if (ThisIDTest != null) { cmdThisIDTest = ((int)commandThisID.ExecuteScalar()); } }//END using (var conn = new SqlCeConnection(dbConnectionString)) 

by privatly via /r/csharp

Leave a Reply