I have a security_role_cd column in database of smallint
datatype. I am selecting this column using following code into a nullable int
variable.
I am getting following error:
Error 3 Type of conditional expression cannot be determined because there is no implicit conversion between 'null' and 'short'
What is the correct code to overcome this error?
SELECT R.security_role_cd FROM Security_Role R WHERE security_role_name = 'Admin'
C#
int? roleID = null;
string commandText = "SELECT R.security_role_cd FROM Security_Role R WHERE security_role_name = @roleName";
SqlCommand command = new SqlCommand(commandText, connection);
command.CommandType = System.Data.CommandType.Text;
command.Parameters.AddWithValue("@roleName",roleName);
SqlDataReader readerRole = command.ExecuteReader();
if (readerRole.HasRows)
{
while (readerRole.Read())
{
roleID = readerRole.GetInt16(0) == 0 ? null : readerRole.GetInt16(0) ;
}
}
readerRole.Close();