wpf 该字符串未被识别为有效的 DateTime。有一个从索引 0 开始的未知单词。标签到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17492269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:09:36  来源:igfitidea点击:

The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. label to string

c#.netsqlwpf

提问by user2122032

What does this error mean? I am trying to retrieve the date from a label and display it on a wpf form.

这个错误是什么意思?我正在尝试从标签中检索日期并将其显示在 wpf 表单上。

          printResident.DateOfBirth = DateTime.Parse( lblDOB.ToString() );

This method calls the retrieve method that executes the sql select from the database to reteive the information onto the form.+

该方法调用retrieve 方法,该方法从数据库中执行sql select 以将信息检索到表单上。+

          ResidentData.Retrieve(printResident.ResidentID);

回答by Gjeltema

As a guess, you're using a Labeldirectly. Try doing lblDOB.Content.ToString()instead.

作为猜测,您正在使用Label直接。尝试做lblDOB.Content.ToString()

回答by vivat pisces

Assuming lblDOBis a label, you'll need to use:

假设lblDOB是一个标签,你需要使用:

lblDOB.Content.ToString();

Initially, you were using lblDOB.ToString(), which returns a string representation of your label (probably something like System.Windows.Controls.Label, followed by the content of your label), not something that DateTime.Parsewould be able to work with.

最初,您使用的是lblDOB.ToString(),它返回标签的字符串表示(可能类似于 System.Windows.Controls.Label,后跟标签的内容),而不是DateTime.Parse可以使用的内容。

Now you're trying to use lblDOB.Content.ToString(), which is the correct way, but you're getting a null reference exception. Place a breakpoint on the statement that loads the date from the database into your label and make sure it's working, because it's complaining that lblDOB.Contenthas no value.

现在您正在尝试使用lblDOB.Content.ToString(),这是正确的方法,但是您收到了空引用异常。在将日期从数据库加载到标签的语句上放置一个断点,并确保它正常工作,因为它在抱怨lblDOB.Content没有价值。

回答by Vishal

May be this Helps:-

可能是这个帮助:-

DateTime dt = DateTime.Parse("01/01/0001 00:00:00");
            label1.Content = dt.ToString();