Click here to Skip to main content
15,917,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int Month = DateTime.Now.Month - 1;

for (int i = Month; i <= 11; i++)
{
   Month++;
   using (DataTableAdapters.AttendanceRegisterTableAdapter Reg = new RRHFOEM04.DataTableAdapters.AttendanceRegisterTableAdapter())
   {
      Reg.AttendanceRegister_Insert(txtStudentID.Text, Convert.ToString(DateTime.Now.Hour + ":" + DateTime.Now.Minute), Convert.ToString(Month), "2014", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
   }
}


Above code are insert only current year month... but i want to insert next 24 month from current month
Posted
Updated 19-May-14 4:09am
v2

1 solution

Try:
C#
DateTime now = DateTime.Now;
int Month = now.Month - 1;
DateTime twoYearsAhead = now.AddMonths(24);
...
Then save that value.

(It's a good idea to only read the current time once when you need it, or it can cause intermittent odd errors which are very difficult to track down if the date or time changes between fetches, as happens at the end of each minute for example.
 
Share this answer
 
Comments
Harnis Findoliya 19-May-14 9:54am    
its right.... but i want to insert only month.... twoYearsAhead is print the allover date... so how can i do that ?
Harnis Findoliya 19-May-14 9:59am    
i want to insert month and year in different column...
OriginalGriff 19-May-14 10:35am    
Um...you are supposed to think about what to do, not just ask and expect an answer - particularly when the code you are using already does 99% of it...
So, a Very Big Clue: What you you think the DateTime.Month property returns in your original code?
int Month = DateTime.Now.Month - 1;
Is there anything there you could use? :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900