I want to do auto generate the value of a field which type is int
. Below is my code and it works successfully when I entry one column data manually in database.
But when the DB is totally empty then the output of this query is 'null'. I want when the output of this query is 'null' then the value of createCode
will be 0 (zero).
ConnectionObj.Open();
string query = "SELECT MAX(t_code) FROM teacher_table";
CommandObj.CommandText = query;
int createCode = Convert.ToInt32(CommandObj.ExecuteScalar());
if (createCode == 'null') //can't compare string and int
{
createCode = 0;
return createCode;
}
else
{
return createCode;
}
How I can do it?