The problem is the while
-loop. It's not an if
, so it is possible that the loop is never entered if there are no rows.
The simplest solution is to assign a default value to the return variable:
string lienBaseConnaissance = null;
// .... at the end of the method:
return lienBaseConnaissance;
Since (i assume that) you are reading a single value this is also possible:
if(_ReaderLines.Read())
{
if (!_ReaderLines.IsDbNull("ParStrP1"))
{
lienBaseConnaissance = _ReaderLines.GetString("ParStrP1");
return lienBaseConnaissance;
}
else
{
return null;
}
}
else
return null;
Note that you should also use the using
-statement to ensure that unmanaged resources are disposed (e.g. the connection gets closed). Use it for everything that implements IDisposable
, so the connection, the command and the reader.