Error Description:
you may write a stored prcedure which return bigint value, the code can be :
SELECT CAST(SCOPE_IDENTITY() AS BIGINT)
The above statement returns the affected rows of tables and the return value is Big int. In the .cs file, you may write code to get the return value of this stored procedure:
int id = (int)sqlCommand.ExecuteScalar();
I'm very sorry that the very strange error happen. We'll get the error: System.InvalidCastException: Specified cast is not valid. at....
Solution:
I don't know why this error happen. We can write code as follows to fix the erorr:
int id = (int)(long)sqlCommand.ExecuteScalar();
Why?
the sql command return an object, we firstly cast it into "long" type, and then cast it to "int" type, however, why can we cast it into "int" type directly? I'm so confused, Who can tell me the reasons?
Thank you very much in advanced.