Both sentences are correct. However, "I-am dat cartea" means I gave the book to him/her, it's not entirely specified to whom. I-am dat lui cartea=I gave the book to him; I-am dat ei cartea=I gave the book to her.
The second sentence sounds better, and I think it's "more correct" (if I can say that) I-am dat ei cartea.