Link to home
Start Free TrialLog in
Avatar of kahvedzic
kahvedzicFlag for Bosnia and Herzegovina

asked on

date time format on localhost and iis server

Hi,
FormView TextBox (insert mode) is bound with field in sql table like this:

Bind("Installed_Date", "{0:dd.MM.yyyy}")

Problem is when I enter date format like 30.01.2010 got error

String was not recognized as a valid DateTime.
Exception Details: System.FormatException: String was not recognized as a valid DateTime.

and data wont be inserted in table.
But if I enter 01.30.2010 insert is OK in ReadOnly mode date is 30.01.2010 and in sql table is the same date. Just to mention that text box in ReadOnly mode is bound same way Bind("Installed_Date", "{0:dd.MM.yyyy}").
How to enable insert with date entered in dd.mm.yyyy format and not with starting month?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of RameshS
RameshS
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Add asp:RegularExpressionValidator control to validate user's input for time.

<asp:RegularExpressionValidator runat="server" id="revDate" controltovalidate="txtDate" validationexpression="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$" 
errormessage="Please enter date in dd.MM.yyyy format." />

Open in new window

Alternatively, you can try this code to accept date in dd.MM.yyyy format. Import using System.Globalization namespace in order to run this code.

       

     CultureInfo cultInfo = new CultureInfo("en-GB", true);
     DateTimeFormatInfo formatInfo = cultInfo.DateTimeFormat;
     formatInfo.ShortDatePattern = "dd.MM.yyyy";
     formatInfo.LongDatePattern = "dd MMMM yyyy";
     formatInfo.FullDateTimePattern = "dd MMMM yyyy HH:mm:ss";
     DateTime date1 = DateTime.Parse("30.01.2010", formatInfo);

Open in new window

Just try this too

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Installed_Date","{0:dd/MM/yyyy}") %>'
Avatar of kahvedzic

ASKER

@RameshS


I just add this to web.config:

<globalization
       requestEncoding="utf-8"
       responseEncoding="utf-8"
       culture="en-GB"
       uiCulture="en-GB" />


Now I can add dd.mm.yyyy when inserting new data to table, I can update and see dd.mm.yyyy as date format in FormView, but in my database I have mm/dd/yyyy and I also need to have same format (dd.mm.yyyy) in sql table. What to do?
The database will always have only one date format and it is normally based on local date time settings(regional settings) of the server/PC where the SQL server datase is running.  You don't need to worrky about the format in which the db is storing the data. You need to convert the date formate when you present the data to the user  through your UI application. You may be able to change the datetime format to 'dd/mm/yyy' format permanently by using the following sql server command.

set language 'british english'