Error converting date for Calendar in asp.net - 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.

Related

match datetime pattern with spring DatetimeFormat annotation

I'm trying to use this jquery input mask plugin to submit a date time field.
The input is being submitted as:
081220151530 - which is 3:30pm 08/12/2015
or in am/pm it's submitting
081220150330p
I'm trying to bind that value to a Joda DateTime field and I think I should be able to specify a custom pattern on the field annotation.
But this pattern's not matching and throwing a validation fault:
#org.springframework.format.annotation.DateTimeFormat(pattern = "DDMMYYYYhhmm")
private DateTime followUp;
The error:
Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'followUp'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type #javax.persistence.Basic #org.hibernate.annotations.Type #org.springframework.format.annotation.DateTimeFormat org.joda.time.DateTime for value '081220151230'; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse "081220151230": Value 22 for monthOfYear must be in the range [1,12]
So 2 questions:
1. Ideally, it could parse the am/pm value, so is there a pattern that will match the am/pm style?
If not, what's the right pattern string to match the 24 hour version?
Update to original question:
So I figured out the 24H pattern, using the DateFormat pattern ddMMyyyyHHmm
But the second part I can't get - so I'm going to reassign this to the jquery-inputmask tag because the fact that it's submitting the am/pm as a and p respectively maybe a bug....
So I think I can solve this if someone knows how to make it submit pm/am instead?
try: ddMMyyyyHHmma
post the value like 05052016

How can i Convert the DatePicker Input to string

I am using the following Codes to get the date from the date picker
objDD_ml.EffDate=(DirectCast(drv.Cells(3).FindControl("rdpEffDate"), RadDatePicker).DateInput).ToString
But it does not return the date... It returns like 'DateInput'
How can i use the code to get the value for Effdate as a correct format of date
Anyone please help me..
Whenever you want date into string and in specific format you can always apply following :
objDD_ml.EffDate = (
DirectCast(drv.Cells(3).FindControl("rdpEffDate"), RadDatePicker).DateInput
).ToString("dd/MM/yyyy")
Try to cast the FindControl object into the corresponding ASP control externally. Then access the value of that control and convert it to .ToString
I don't know what control you are using, but it should be something like below -
objDD_ml.EffDate = ((RadDatePicker)(drv.Cells[i].FindControl("rdpEffDate")).SelectedDate.Tostring;

best date regex mm/dd/yyyy

please help me to find the best regex for the dateformat mm/dd/yyy and m/d/yyyy.
i have tried different links but everyone has some problems. so please help me to solve this.
What is the MM/DD/YYYY regular expression and how do I use it in php?
http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5
http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html
Use one of the DateTime.TryParse overloads (or DateTime.TryParseExact if you want more control) to validate whether a string is a valid representation of a DateTime.
DateTime dt;
if(DateTime.TryParse(someInputString, out dt)
{
// it is valid and dt can be used
}
This would be a better approach than regular expressions - it is faster, well tested and will produce a valid DateTime object.
If you need a regular expression then something like this would work (for most cases, since it allows 31st of February).
(0?\d|1[012])\/([012]?\d|3[01])\/\d{4}
Usually it is much better to use DateTime parsing for verifying date formats. Like DateTime.TryParseExact method. You can use the regular expression in the browser (in JavaScript) but you should really use DateTime parsing on the server side.

Test if the user typed email format ASP.NET (VB)

I have a TextBox, and I want to force the user to type an email format in this field like (example#mail.com) ?
I don't want to use FilteredTextBoxExtender or the RegularExpressionValidator.
I want to do it manualy.
Use the MailAddress class of System.Net.Mail. If what you pass into it is not a valid email address it will fail.
Example:
How do I validate email address formatting with the .NET Framework?
You are really going to reinvent the wheel.
But if it is your wish, you have to use string manipulation functions built in to the String object.
First do a check whether there is a in # symbol in the text.
Use String.Contains to check that.
Or you can use String.IndexOf to check whether the # symbol is present, and if present which index is it present. (considering the string as an array of characters)
And then check whether there are any (and how many) characters present before the # symbol.
If the symbol # symbol was in the 4th index, then you know there are 3 characters before etc.
There's plethora of functions for the String object. You may have to use Length function and String.SubString to retrieve parts of the string.
Get the indexes of the # symbol and the . symbol and check whether there are at least 3 characters in between.
I really cant seem to think of all the possibilities but first list down all the possibilities and check them one by one.
You can also use the Contains method to check whether illegal characters are present :)
EDIT: String.LastIndexOf will return the last index where a specified character was found ;)
And you count and check whether the # symbol was found more than once etc
String.IndexOfAny(Char[])
String.IndexOfAny Method (Char[], Int32)
String.IndexOfAny Method (Char[], Int32, Int32)
This is the best way I found on internet.
Regex.IsMatch(YourStringEmail, "^(?("")("".+?""#)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])#))" + _
"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$")
Thank you.

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.

Resources