I can't see anything wrong with your code. The only reason I can think of why this would not work is if a previous write hadn't finished, try adding while(EECON1bits.WR); to the start of your code.
Thank you Pommie for the suggestion, i tried it and it but the problem still exists. However i noticed that if i comment everything in my main() function, it is still giving errors.
Attached is the errors (warnings) from simulation.
Can it be that i have to setup some register in main function.
It is giving all these errors even with main function such as