You can't return byte data there: it doesn't exist, because you haven't read it.
You only read any data when there is at least one row available - and there should be only one row, so the
while
loop is redundant, just use an
if
instead - and when you have retrieved it you immediately return it to the caller. So what data do you expect to be available to return if there are no rows as a result of the query?
What you are asking for is "if there is no such employee, return his picture". Which is clearly nonsense!
Instead, either return an empty array, or return
null
and let the calling method figure out what it wants to do with the problem!
public static byte[] GetEmpImg(int emp_no)
{
byte[] empImgBytes = null;
AdoHelper.ConnectionString = ConfigurationManager.ConnectionStrings["imageconst"].ConnectionString;
string query = "SELECT EmpPhoto FROM EmpPhoto WHERE EmpNo=@EMPNO";
using (AdoHelper adoHelper = new AdoHelper())
{
using (SqlDataReader dataReader = adoHelper.ExecDataReader(query, "@EMPNO", emp_no))
{
if (dataReader.Read())
{
empImgBytes = (byte[])dataReader["EmpPhoto"];
}
}
}
return empImgBytes;
}