I have been having this issue with dates since "ever"
I live in a country where we use the british date formats e.g. dd/mm/yyyy but everytime I try to do a query to a sql db hosted in a US server, 100% of the time I come accross to errors.
Convert.ToDateTime("2007-17-5") produces an error where as Convert.ToDateTime("2007-5-17") also produces an error.
Many validation methods, many t-sql queries and many other ways I've tried and solved partially in most of my projects however, I would like to know if anyone out there has a universal way of converting strings to a date that would not cause any problems?
or any good resources regarding working with dates ?
----------- editing... -----------------
even if I format the dates correctly, the query to the sql server doesn not produce any value even if I am sure that there are.. for example.. if I am searching for the records that belongs to dates between 1/1/2009 (that is 1 Jan 2009) through 1/5/2009 (that is 1 MAY 2009) no records are returned. And when I try to change the date in the query builder of the sql server, it gives me an error saying its not a valid date that I am entering...
Use DateTime.TryParseExact and give some format, that represent your date string
2007-5-17 is not a valid International Date (ISO 8601), 2007-05-17 is. If you are going to be using the International format CCYY-MM-DD you need to include the leading 0s
http://www.w3.org/QA/Tips/iso-date
http://www.saqqara.demon.co.uk/datefmt.htm
You handle a lot date validation of this in the UI.
First, in your webconfig, you need to set your culture:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" />
You can use the CompareValidator control on the client side with by setting the Type attribute to "Date".
<CompareValidator ID="id" runat="server"
ControlToValidate="controltovalidateid"
ErrorMessage="Invalid Date"
Type="Date" />
To limit the users options I've used the calendar control in the ASP.NET. One nice thing about that control is you can set the date format in the attribute:
<ajaxToolkit:Calendar runat="server"
TargetControlID="Date1"
CssClass="ClassName"
Format="dd/mm/yyyy"
PopupButtonID="Image1" />
Other information
AJAX Control Toolkit Download
Calendar Control: AJAX Control Toolkit sample
Compare Validator
Related
I've written an SQL Server stored procedure that, amongst other things, accepts a date parameter:
CREATE PROCEDURE data.addScore
... (some parameters),
#testDate date
AS
On an ASP.NET page I have a textbox with a Calender Extender control (from the AJAX Toolkit) that allows the user to select a date; the date is stored in the textbox in the format DD/MM/YYYY. I have some code that is supposed to execute the stored procedure with the parameters, using the date value from the textbox:
cmd.Parameters["#testDate"].Value = Convert.ToDateTime(((TextBox)li.FindControl("date")).Text);
I know I can definitely get to the textbox's value using ((TextBox)li.FindControl("date")).Text, but for the life of me I can't get it into a format that doesn't throw up one of a few errors, mainly 'Input string was not in the correct format'.
When I run the stored procedure directly on the server I usually bind the parameter like #testDate = 'YYYYMMDD'.
I would suggest you don't store anything in the textbox using regional, ambiguous formats like dd/mm/yyyy. If you use an unambiguous format like YYYYMMDD, you're not going to have any issues. And if users are picking from a calendar control or something, it should be easy to separate the date you present to them from the date you pass to the database.
<asp:Textbox id="date" runat="Server"></asp:Textbox>
<cc1:CalendarExtender ID="calwhendate" runat="server" Enabled="true" Format="YYYYMMDD"
TargetControlID="date"></cc1:CalendarExtender>
Change your Ajax Calendar Control to your desired format
which is better convert the date by database or c# to bind it on grid event rowDataBound ? ,
and why & when we use this or this ?
If you are converting the date to some other date, it would be more efficient to do the conversion in C# and save the database some processing.
If you are trying to format the date, use the DataFormatString on the bound field.
You don't "convert" the date on the scenario you describe; you simply format it according to your needs. The date should be a date type on the database side and on the presentation layer, whatever it may be, can be displayed according to your needs. For example in asp.net you can format your date on the GridView as follows:
<asp:boundfield datafield="SomeDate" DataFormatString="{0:MM/dd/yyyy}" />
or
<asp:boundfield datafield="SomeDate" DataFormatString="{0:dd/MM/yy}" />
Database date format: "yyyy-MM-dd"
C# default date output: "MM/dd/yyyy"
My date format: "dd/MM/yyyy"
How do I use the asp:SqlDataSource with an asp:ControlParameter connected to an asp:TextBox using my date format? Specifically, I wanted to know
if there was a way to do this without having it done in code-behind
if I'm bound to code-behind, how to do it?
I've read about the Culture class, but I'm not sure that's the right path to explore. I've also had a check on some stackoverflow posts and came accross this Binding Date of DateTime in a control, but I've never seen this structure of Text formatting before in ASP.NET.
Thanks in advance,
I have an ASP.Net 3.5 page that contains a FormView control, bound to a Business Object using an ObjectDataSource.
One of the properties of the business object is a DateTime type, and I want to do 2 way databinding for this object, including the DateTime property.
I use a custom format for displaying the DateTime property, as shown here:
<asp:TextBox ID="TextBoxDate" runat="server" Text='<%# Bind("Date", "{0:d MMM yyyy HHmm}") %>' />
It displays just fine.
The problem is when I attempt to perform an Update. I get the following exception:
String was not recognized as a valid DateTime.
My ObjectDataSource contains an explicitly set UpdateParameter for this Property, but it doesn't appear to make any difference.
<UpdateParameters>
<asp:Parameter Name="Date" Type="DateTime" />
</UpdateParameters>
What am I doing wrong?
UPDATE:
It turns out that if I change my format string in my Bind expression to
{0:d MMM yyyy HH:mm}
(Notice the colon between the HH and the mm)
... then the two way data-binding works as expected. Sadly, this isn't exactly what I wanted. I was hoping to use the 24 hour clock without a colon, hence my original format string. This is still not working, and I would love to know why? And even better, I'd love to know how I can work-around this shortcoming in the framework, but still do declarative databinding.
Thanks.
You could override the ItemUpdating event of the FormView control and then modify the value for that parameter to make sure it's in the right format. Most likely this means:
get the value from e.NewValues("Date")
Parse the string value into a DateTime object
assign the value back to e.NewValues("Date")
I've had to do something like this in the past with currency fields where people might enter a dollar sign which would cause errors if left alone.
Bind() method, actually provide two way data binding and Eval provide one way data binding
In your scenario, you have to modify date in your formview databound event when you are showing data to user rather setting date format in bind property.
In this way you will get not error when update your fields.
I have a database that contains a date and we are using the MaskedEditExtender (MEE) and MaskedEditValidator to make sure the dates are appropriate. However, we want the Admins to be able to go in and change the data (specifically the date) if necessary.
How can I have the MEE field pre-populate with the database value when the data is shown on the page? I've tried to use 'bind' in the 'InitialValue' property but it doesn't populate the textbox.
Thanks.
We found out this morning why our code was mishandling the extender. Since the db was handling the date as a date/time it was returning the date in this format 99/99/9999 99:99:99 but we had the extender mask looking for this format 99/99/9999 99:99
Mask="99/99/9999 99:99:99"
the above code fixed the problem.
thanks to everyone for their help.
Are you referring to the asp.Net Ajax toolkit extensions at:
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx
If so have you checked that your data is coming back in the correct format? It will have to match your date format in order to be displayed.