The string was not recognized as a valid datetime - asp.net

objfile.dateFileDate=convert.ToDatetime(Format(txtdate.text,"MM/dd/yyyy hh:mm"))
following error is coming
The string was not recognized as a
valid datetime .There is an unknown
word starting at 0.
What should i do to save this datetime,
please help

You can't format normal text using datetime formats.
Try
C#
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", null);
VB.NET
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", Nothing)
This is assuming dateFileDate is a DateTime type and that the txtdate.text is in the above format.

If your program is used by a international crowd, read on :)
ppl from different cultures will write dates in diffrent formats, so if your always going to parse the string that could be get sticky. Consider using the calander control?
Im saying this based on personal experience.
Also finding out why your current one is failing, i would do a DateTime.Now.ToString() and compare that to whats in the textbox so you can see whats curently being typed in wrong ( While debugging off course, to help track down the problem)

Try hh:nn instead of hh:mm
I believe mm is Months in two digit format and nn is minutes in two digit format.

Related

decimal.parse fails for currency string created with String.Format?

I have a field that I display via:
String.Format({0:c},amount)
This produces the string "$28.28"
However, when I try to convert back to a decimal amount, I get an incorrect format exception:
amount = Decimal.Parse(amount.Text, NumberStyles.Currency)
I also tried it with NumberStyles.AllowCurrencySymbol with the same results. I verified that the value in amount.Text is "$28.28".
Am I missing something? Shouldn't these two operations use the same currency symbol and formats?
var amount = decimal.Parse("$28.28", NumberStyles.Currency);
works fine for me. Make sure your input string is what you think it is.
Are these on different machines?
The machines be be setup differently.
The default formats are set in the control panel regional settings.

DateTime syntax reference

I understand there are no complete reference docs available for Razor as yet, but would like to know what the various date format options are. Such as:
DateTime.Now.Day
DateTime.Now.Month
DateTime.Now.Year
DateTime.Now.Date
DateTime.Now.TimeOfDay
DateTime.Now.AddDays(1)
etc etc
Is there a complete reference anywhere for DateTime?
How do I get just DD/MM/YYYY without 00:00:00 being appended for example?
The DateTime class is part of the .NET framework. See MSDN documentation.
To format dates, you can use the .ToString(string format) method:
#DateTime.Now.ToString("dd/MM/yyyy")
DateTime is actually a .NET type. The full documentation is available here: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
For your particular question you would want to use the ToShortDateString() method:
DateTime.Now.ToShortDateString()

Error converting date for Calendar in asp.net

I have written code for two checkboxes:
string sdate= Convert.ToDateTime(txtFromDate.Value);
string edate=Convert.ToDateTime(txtEndDate.Value);
I am getting the following error: "String was not recognized as a valid DateTime".
Well, it's reasonably clear: the input string wasn't in an appropriate format. I suggest that instead of using Convert.ToDateTime, you use DateTime.TryParseExact and specify the format string (or strings) that you expect the user to enter.
Using TryParseExact instead of just ParseExact means you can detect if the user has entered an incorrect date without an exception being thrown - and you should check for that. Basically the return value of TryParseExact indicates success or failure, and an out parameter is used to capture the parsed date/time on success.
What I hadn't noticed to start with is that you're then trying to assign a DateTime value to a string. That's not going to work - but we can't advise you on what you should be doing instead without knowing what you want to do with the data. I suspect you want to change sdate and edate to be DateTime variables instead.

Conversion from string to double error

i have a project for uploading video.in that when i click the showvideo button there is a error.
code-
param name="url" value='<%# "VideoHandler.ashx?FileID=" + Eval("FileID") %>'
error message ::: conversion form string="VideoHandler.ashx?FileID=" to type 'Double' is not valid
anyone knows please answer for me thank you
"VideoHandler.ashx?FileID=" is a string. Eval("FileID") results in a double. You have a type mismatch, so the addition overload doesn't know how to proceed. Solve it like this:
string.Format("VideoHandler.ashx?FileID={0}", Eval("FileID"))
Without seeing the code, it sounds like you are trying to convert a string which isn't a valid double. Are you taking the value of the query string and trying to convert it or, could you have accidentally tried to convert the page name along with the query string? Based on the short error message you gave, that is what it looks like. If you post the code which is doing the conversion, that will likely make it clearer what is going on, but that is my best guess at the moment.
You are trying to convert a string which is not a valid double type.
I think you are trying to convert the FileID field to double. Then you can split the string and then convert only the FileID part of it.
You can get the querysting data using
Request.QuerySting["FileID"] and then convert it to double.
or use
Double.TryParse Method
We should see some code. Apparantly the application is trying to convert "VideoHandler.ashx?FileID=" to a Double value which cannot be done.
Just add ToString() to the end of your Eval. FileId is a double, and it's seeing the + and trying to add it to a string, numerically, instead of concatenating it.

(ASP.NET) How do I remove special characters when doing a DateTime.Now.ToString()

So I have a flashobject which I need to pass a formatted DateTime string to.
My code:
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
which outputs as: 2009-09-16 22:26:45
However when it is actually output to HTML and swfobject it renders it as:
so.addVariable("inNowDate","2009-09-16+22%3a25%3a13");
I think this is messing up a calculation that the flash object does based off the current time. Do I need to encode or decode this?
Any help would be greatly appreciated! Thanks!
It's not that you have gained special characters, but rather certain special characters you already had are now URL encoded.
There's not enough information present for me to see exactly where this URL encoding his happening. Can you post a bit more context?
When you output to html, try using UrlDecode.
http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

Resources