[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2051
  • Last Modified:

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
0
kahvedzic
Asked:
kahvedzic
1 Solution
 
RameshSCommented:
You can do it by changing the culture settings in web.config. Change the culture to "en-GB" in web.config.

For example,

   
      <configuration>
   <system.web>
      <globalization
         requestEncoding="utf-8"
         responseEncoding="utf-8"
        culture="en-GB"         uiCulture="en-GB" />
   </system.web>
</configuration>  
 
0
 
lazyberezovskyCommented:
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

0
 
RameshSCommented:
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

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
masterpassCommented:
Just try this too

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Installed_Date","{0:dd/MM/yyyy}") %>'
0
 
kahvedzicAuthor Commented:
@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?
0
 
RameshSCommented:
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'

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now