Click here to Skip to main content
16,012,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have got two TextBoxes(txtdateofissue and txtdateofexpiry).The First Textbox is Attached with a CalendarExtender control.
I want that if a user selects a date for the txtdateofissue textbox then automatically the same date but next year should get entered in txtdateofexpiry textbox.
Date Should be in dd-MM-yyyy format i.e. 24-04-2013.
i have written a code for this but i am getting an error (String is not a valid datetime format)

my code:

C#
protected void txtdateofIssue_TextChanged(object sender, EventArgs e)
        {
            DateTime dt = Convert.ToDateTime(txtdateofIssue.Text);
            dt = dt.AddDays(-1).AddYears(1);
            txtdateofExpiry.Text = dt.ToShortDateString();
        }


the first line in which i declare datetime variable dt throws up the error (String is not a valid datetime format)

when i run this code in my local machine it works fine but if i run it on iis then it shows errors...please either correct my code or guide me in a new way...thanks in advance...
Posted
Comments
Rockstar_ 24-Apr-13 0:54am    
On other system or same system?
avinash_thakur86 24-Apr-13 1:01am    
on other system
Rockstar_ 24-Apr-13 1:02am    
Check the date format of that system and ur system, may be it is different.

Don't use all those Convert methods, try to avoid them in general. Do what you really mean to do — parsing. By two reasons: 1) the word "parse" helps you to understand what you really do; 2) parsing methods allows you to add parameters to specify format/culture of the string presentation of time, according to the string you expect.

You need to use one of the methods of the structure System.DateTime: Parse, TryParse, ParseExact or TryParseExact. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

For format specifiers, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

—SA
 
Share this answer
 
C#
DateTime dt;
if (DateTime.TryParseExact(txtdateofIssue.Text, "dd-MM-yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None,out dt))
{
   dt = dt.AddDays(-1).AddYears(1);
   txtdateofExpiry.Text = dt.ToString("dd-MM-yyyy");
}
 
Share this answer
 
v2
set Format="dd/MM/yyyy" attribute for calendar extender and change ur textchanged event as follows

C#
protected void txtdateofissue_OnTextChanged(object sender, EventArgs e)
   {
       string[] str = txtdateofissue.Text.Trim().Split('/');
       System.DateTime dt = new System.DateTime(int.Parse(str[2]), int.Parse(str[1]), int.Parse(str[0]));
       dt = dt.AddDays(-1).AddYears(1);
       txtdateofexpiry.Text = dt.ToShortDateString();
   }
 
Share this answer
 

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