I get a specified cast not valid error when I execute a stored proc like this:
return (int)comm.ExecuteScalar();
When I execute it in SQL Server it returns 1, so I know my proc is working.
What is wrong with my cast?
Updated:
public static int IsPresent(string userName, int inTime)
{
SqlConnection connObj = new SqlConnection();
connObj.ConnectionString = Util.SQLConct();
connObj.Open();
SqlCommand comm = new SqlCommand("usp_IsUserLocked", connObj);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@Username", userName));
comm.Parameters.Add(new SqlParameter("@InTime", inTime));
return (int)comm.ExecuteScalar();
}
Thanks in advance
It is possible that your method returns double or int64 or any other type which cannot be implicitly casted to (int). Use
Convert.ToInt32to ensure that your method returns the right type.