Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This Script Can change my Date but my month and year are not chane..........

This code set the Date= 2 sept 2013
but I want = 2 oct 2013
Day change but Months don't change.

C#
int day = System.DateTime.Now.Date.AddDays(2).Day;
int month = System.DateTime.Now.Date.Month;
int year = System.DateTime.Now.Date.Year;

ddlDay.Items.FindByValue(day.ToString()).Selected = true;
ddlMonth.Items.FindByValue(month.ToString()).Selected = true;
ddlYear.Items.FindByValue(year.ToString()).Selected = true;
Posted
Updated 29-Sep-13 23:37pm
v8
Comments
[no name] 30-Sep-13 1:30am    
I didn't get you, could you please improve question?
[no name] 30-Sep-13 1:56am    
Not getting your qn. plz update it.
Nelek 30-Sep-13 5:34am    
Get used to write proper english please. SMS-Text makes your message look like not serious, on the other hand... not all people are english speakers and may not understand what you say
Parmendra choudhary 30-Sep-13 5:38am    
will u please check my code

1 solution

Don't mess about with integers - use a DateTime value instead:
C#
DateTime today = DateTime.Now.Date;
DateTime tomorrow = today.AddDays(1);

You can then use tomorrow.Day and so forth.
 
Share this answer
 
Comments
Parmendra choudhary 30-Sep-13 2:17am    
Same Exception
Parmendra choudhary 30-Sep-13 2:19am    
This code work properly but in the last day of months its throw exception
OriginalGriff 30-Sep-13 2:25am    
What exception?
You haven't mentioned any exception!
Parmendra choudhary 30-Sep-13 2:26am    
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 42: int year = System.DateTime.Now.Date.Year;
Line 43: int DayTommorow = day+2;
Line 44: ddlDay.Items.FindByValue(DayTommorow.ToString()).Selected = true;
Line 45: ddlMonth.Items.FindByValue(month.ToString()).Selected = true;
Line 46: ddlYear.Items.FindByValue(year.ToString()).Selected = true;
Parmendra choudhary 30-Sep-13 2:27am    
exception only for " ddlDay.Items.FindByValue(DayTommorow.ToString()).Selected = true;" this line

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