[Solved] Error not all code paths return a value?


public Int64 id(string fd, string tb)
{
    Int64 I = 0;

    if (con.State == ConnectionState.Open)
    {
        con.Close();
    else
    {
        con.Open();
    }

    SqlCommand cmd = new SqlCommand("SELECT MAX (" + fd + ") FROM " + tb + "", con);
    cmd.CommandType = CommandType.Text;
    if (con.State == ConnectionState.Closed)
    {
        con.Open();
        I = Convert.ToInt64((cmd.ExecuteScalar().Equals(DBNull.Value) ? 0 : cmd.ExecuteScalar())) + 10;
    }
    return I; 
}

The return I statement was inside the if (con.State == ConnectionState.Closed) block. This means it wouldn’t have been called it the con.State wasn’t closed and the function wouldn’t have returned anything. I is initialised to 0 at the top of the function, so this will be returned if the SQL query isn’t run.

1

solved Error not all code paths return a value?