When trying to comfort someone during an emotionally difficult time (particularly during times of grief), it's important to simply let him know that you care about him. Therefore, you could tell him, for example, "I'm here for you," "I'm listening" and "I love you." You could also tell him that you don't know what to say, or even say nothing and comfort him with your presence. Your actions speak louder than your words.
In addition to this, however, it's never a good idea to talk about yourself too much, even if you've gone through the same things that your friend is currently going through. The focus should be on him if he needs comfort and sympathy, not on you. You could, instead, say something such as, "I've dealt with something like this before and I wanted to express my sympathy. How are you doing?"
It's also important to keep your religious opinions to yourself unless your friend mentions religion first. Therefore, it's also not a good idea to say things such as, "It's God's will" or, "God doesn't give you more than you can handle." Since your friend is going through such a tough time, saying things like that makes you seem very insensitive even if you say them with the best intentions.