Solved

ASP.NET: Check date input

Posted on 2008-10-07
6
895 Views
Last Modified: 2013-12-17
I am importing data from a excel file and I have to check the date format in one column. I use the code in the snippet but it does not work. How can I correct that?
//I want to check if the date is in format 'yyyymmdd'
DateTime entryD
CultureInfo cult = new CultureInfo("en-US");
DateTimeStyles stl = DateTimeStyles.AssumeLocal;
if (!DateTime.TryParse(row["EntryDate"].ToString(),cult, stl, out entryD)) //This always pass, even if the date is in ddmmyyyy format
{
//Do other processing here
}

Open in new window

0
Comment
Question by:karakav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Expert Comment

by:Didier Vally
ID: 22657240
You can use date validation in ASP.NET :

<asp:Label runat="server" AssociatedControlID="tbDate" Text="Date" /> <asp:TextBox ID="tbDate" runat="server" /> <asp:CompareValidator runat="server" ControlToValidate="tbDate" Text="*" ValidationGroup="MyValidationGroup" Operator="DataTypeCheck" Type="date" />

Open in new window

0
 
LVL 6

Expert Comment

by:avnish_tanna
ID: 22657528
check if below link helps:
http://www.daniweb.com/forums/thread34168.html (see post #7)
0
 
LVL 4

Author Comment

by:karakav
ID: 22657534
I won't use a compare validator to compare values in a file.?!?!?
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 14

Expert Comment

by:Binuth
ID: 22657689
use DateTime.ParseExact
	try 
	{	        
		// it will throw expection if string format is not yyyyMMdd
		DateTime entryD = DateTime.ParseExact(row["EntryDate"].ToString(),"yyyyMMdd",null);	
	}
	catch (Exception)
	{
		
		throw;
	}

Open in new window

0
 
LVL 4

Author Comment

by:karakav
ID: 22657887
I finally managed to do it this way:
//I want to check if the date is in format 'yyyymmdd'
DateTime entryD
CultureInfo cult = new CultureInfo("en-US");
DateTimeStyles stl = DateTimeStyles.None;
if (!DateTime.TryParseExact(row["EntryDate"].ToString(),"yyyyMMdd hh:mm:ss",cult, stl, out entryD)) //This always pass, even if the date is in ddmmyyyy format
{
//Do other processing here
}

Open in new window

0
 
LVL 3

Accepted Solution

by:
trunghieubkit earned 500 total points
ID: 22657944
Your result does not depend on the 2 lines

CultureInfo cult = new CultureInfo("en-US");
DateTimeStyles stl = DateTimeStyles.None;

"yyyyMMdd" is Japan culture,




DateTime entryD;
CultureInfo cult = CultureInfo.InvariantCulture;
DateTimeStyles stl = DateTimeStyles.AssumeLocal;
if (!DateTime.TryParseExact("20070122", "yyyyMMdd", cult, stl, out entryD))
{
    return;
}

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question