[2.0] Passing Date to property
Hi all :wave:
I am passing a date in the property then some time I am getting error that string is not in the valid date format.
In the Text box my date is in "dd/MM/yyyy" Format!
I am working in the web application.
Thanks
C# Code:
eventCalender.Event_Date = Convert.ToDateTime(String.Format("{0:dd/MM/yyyy}",this.txtEventDate.Text.Trim()).ToString());
Re: [2.0] Passing Date to property
You can't format a String as a String in a date format. When you do this:
CSharp Code:
String.Format("{0:dd/MM/yyyy}", X)
X has to be a DateTime object; it can't be a String object. You say that your string is already in dd/MM/yyyy format, so why would you need to use String.Format to put it in that format even if you could? Further, the result of String.Format is a String, so why are you calling its ToString method? You're taking a String, formatting it as a String and then converting it to a String. Does that sound a bit redundant? It should simply be:
CSharp Code:
eventCalender.Event_Date = Convert.ToDateTime(this.txtEventDate.Text.Trim());
That said, unless you are absolutely positive the TextBox contains a valid date a would recommend using DateTime.TryParse rather than Canvert.ToDateTime. The fact that you're using Trim suggests to me that you ae NOT absolutely positive.
Re: [2.0] Passing Date to property
Ok now there is 15/11/2007 in the text box, This is in the format of "dd/MM/yyyy"
I used your suggestion but still it showing the error that string was not in the valid date format.
Thanks
Re: [2.0] Passing Date to property
I'm guessing that it's being run on a system where the standard date format is M/dd/yyyy, in which case the 15 is being interpreted as the month, which is invalid. If you want to parse a specific format regardless of regional settings then call TryParseExact.
Re: [2.0] Passing Date to property
I am looking for the tryprase but not getting the suitable example for tryprase, can you plese provide me some example.
Thanks
Re: [2.0] Passing Date to property
I don't really see that an example is required. Is this not clear:
Quote:
Originally Posted by MSDN
C#
public static bool TryParse (
string s,
out DateTime result
)
Parameters
s
A string containing a date and time to convert.
result
When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.
Return Value
true if the s parameter was converted successfully; otherwise, false.
Re: [2.0] Passing Date to property
But sir if I am confused that what we have to pass in the out DateTime result
Re: [2.0] Passing Date to property
A DateTime object. As the documentation says, it will contain the parsed value after a successful conversion. Have you never used Double.TryParse before?
Re: [2.0] Passing Date to property
Before I am always using the convert.toint, convert.todouble
I never use the tryprase before!
Thanks