Trying to do the following:
public static int GetJobStatusByNumber(int jobNumber)
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(connString);
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = @"select STATUS from JOB
where JOB_NUMBER = @jobNumber";
cmd.Parameters.AddWithValue("@jobNumber", jobNumber);
int result = ((int)cmd.ExecuteScalar());
return result;
}
finally
{
if (connection != null)
connection.Close();
}
}
Thouht I can add the varible 'jobNumber' to the query by using 'AddWithValue' but I'm getting a cast error
Message: System.InvalidCastException : Specified cast is not valid.
What's wrong here?
Thanks.
STATUSan integer?ExecuteScalarreturns the first column value from the first row, regardless of type, so if it gets back, say, a string, then the cast tointwill fail.