Question

I want to make a function which returns a string:

public string LienBaseDeConnaissance(string entreprise)
{
    SqlConnection cnx = new SqlConnection("/* connection string */");
    cnx.Open();
    SqlCommand RequeteExiste = new SqlCommand("sp_SEL_LIEN_BASECONNAISSANCE_EXTRANET_CLIENT", cnx);
    RequeteExiste.CommandType = CommandType.StoredProcedure;

    SqlParameter Parameter = RequeteExiste.Parameters.Add("@nom_entreprise", SqlDbType.NVarChar, 15);
    Parameter.Value = entreprise;

    string lienBaseConnaissance;
    SqlDataReader _ReaderLines = RequeteExiste.ExecuteReader();
    while (_ReaderLines.Read())
    {
        if (_ReaderLines["ParStrP1"].ToString() != null)
        {
            lienBaseConnaissance = _ReaderLines["ParStrP1"].ToString();
            return lienBaseConnaissance;
        }
        else
        {
            return null;
        }
    }
    cnx.Close();   
}

I select data thanks to a stored procedure and then I would like to return it as a string. The problem is that if I don't put a return after the while (), all the code doesn't return a value. However I need the variable lienBaseConnaissance which contains the data and out of the while(), the variable in question doesn't have anymore the value I'm looking for.

Was it helpful?

Solution

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.

OTHER TIPS

Lets say that _ReaderLines.Read() returns false, then the code execution wont enter the while loop, so no return path would be followed.

You need to add a return statement after the cnx.Close(); statement.

Something like

public string LienBaseDeConnaissance(string entreprise)
{
    SqlConnection cnx = new SqlConnection("Data Source=SR8-ICARE-SQL;Initial Catalog=db_GENAPI_CM;User Id='IcareReader';Password='$Genapi.Reader1$';");
    ...


    SqlDataReader _ReaderLines = RequeteExiste.ExecuteReader();
    while (_ReaderLines.Read())
    {
        if (_ReaderLines["ParStrP1"].ToString() != null)
        {
            lienBaseConnaissance = _ReaderLines["ParStrP1"].ToString();
            return lienBaseConnaissance;
        }
        else
        {
            return null;
        }
    }

    cnx.Close();  
    return null; //this line here
}

I would also recomend looking at using Statement for the SqlConnection

Something like

public string LienBaseDeConnaissance(string entreprise)
{
    using(SqlConnection cnx = new SqlConnection("Data Source=SR8-ICARE-SQL;Initial Catalog=db_GENAPI_CM;User Id='IcareReader';Password='$Genapi.Reader1$';"))
    {
        ...     
        SqlDataReader _ReaderLines = RequeteExiste.ExecuteReader();
        ...
    }
    return null; 
}

if the first line does not contain your target variable, the function will return null. Instead you want to move the return null to after the while loop, so that happens only when your target variable is not found at all.

Because if the _ReaderLines.Read() return false at the first iteration of the loop then you don't enter the while loop, but you fall out of and reach the method's exit without any return value.

But your code doesn't reallyn need a loop at all because your intention is to read just the first record and exit immediately, so I could suggest to change your code assigning a default value to the variable lienBaseConnaisance , use an if on the _ReaderLines.Read() and fall to the return point at the end of the method-

    string lienBaseConnaissance = null;
    SqlDataReader _ReaderLines = RequeteExiste.ExecuteReader();

    if(_ReaderLines.Read())
    {
         if (_ReaderLines["ParStrP1"].ToString() != null)
             lienBaseConnaissance = _ReaderLines["ParStrP1"].ToString();
    }
    cnx.Close();   
    return lienBaseConnaissance;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top