simple:
your code:
declare @i int
declare @cdate int
set @i = 0
while (@i < @cdate)
what is value of @cDate? 0! while 0 < 0 .....
something tells me that the line
set @cdate = (select DATEDIFF(dd,@fromdate, @todate))
should be used before the while looop