Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要验证字段日期_C#_Asp.net - Fatal编程技术网

C# 需要验证字段日期

C# 需要验证字段日期,c#,asp.net,C#,Asp.net,我想知道在这种情况下如何实现验证,在这种情况下,我会向用户触发消息,说明应该填充字段 try { if (process == null) process = new Process (); try { DateTime DateSep = DateTime.Parse (line.ItemArray [18] .ToString ()); processInterested.DateSep = DateSep;

我想知道在这种情况下如何实现验证,在这种情况下,我会向用户触发消息,说明应该填充字段

try
{
    if (process == null)
        process = new Process ();   
    try
    {
        DateTime DateSep = DateTime.Parse (line.ItemArray [18] .ToString ());
        processInterested.DateSep = DateSep;
    }
    catch
    {
        logErro.Append ("The cell S" + (line ["index"]) + "must be filled witha date.");
        fileInvalidated = true;
    }
} 
catch (Exception error)
{
    fileInvalidated = true;
    logErro.Append (error.Message + "<br>");
}
试试看
{
if(进程==null)
流程=新流程();
尝试
{
DateTime DateSep=DateTime.Parse(line.ItemArray[18].ToString());
processinterest.DateSep=DateSep;
}
抓住
{
附加(“单元格S+(行[“索引”])+”必须填入日期。”);
fileInvalidated=true;
}
} 
捕获(异常错误)
{
fileInvalidated=true;
logErro.Append(error.Message+“
”); }
与其在DateTime解析中使用try/catch,为什么不直接使用
DateTime.TryParse()
?根
try/catch
是无用的,只要你的
新进程()
没有执行一些可能引发异常的逻辑。maccettura,我从来没有这样使用过,我的情况会是什么样的?奇怪的是,使用.Parse它可以工作,现在不再使用了.CodeNotFound,我在其他方面使用了Process()yes referent,因为我正在使用try中的Date字段,所以我决定缩短代码并只引用我遇到的问题。这里的问题非常不清楚。如果您询问如何显示验证消息而不是记录验证消息,则需要标记一项技术(例如web表单或MVC)。如果您询问如何在DateTime中存储null,则不能,但可以将其存储在诸如
DateTime?
(也称为
Nullable
)之类的文件中。