Select Max(SRN) from Employee
This query returns NULL when SRN has no values
Then it satisfies the condition (rd.hasrows) because it has single record as NULL.
When you trying to convert the null value it shows the error
Object cannot be cast from DBNull to other types
because you cannot convert NULL value into anything .