指定的强制转换对DataReader.GetDateTime无效

时间:2016-02-28 20:02:59

标签: .net vb.net ms-access

尝试在VB.NET上显示MS Access DB的日期时,收到<div ng-messages="{ required: !main.o1ToAdd || !main.o2ToAdd }" ng-if="postForm.o1.$touched || postForm.o2.$touched"> <!-- This message is shown when either of the inputs is missing --> <span ng-message="required">at least 2 are required</span> </div> 错误消息。

我试图显示日期的代码行是

Specified cast is not valid.

我想显示日期。有人可以自由地向我解释我应该如何编写代码来实现这一目标吗?此外,如果你们有一个类似问题的链接以前回答过,我将非常感激。

完整代码:

txtEntyDate.Text = sqlRead.GetDateTime(7)

1 个答案:

答案 0 :(得分:1)

TextBox.Text是字符串,GetDateTime()的回复是DateTime类型,因此您不能只将其分配给另一个。

补救措施A

TextBox2.Text = rdr.GetDateTime(1).ToString("MM/dd/yyyy")

补救措施B

Dim dtTemp As DateTime
...
' how you would read it into a DT var:
dtTemp = rdr.GetDateTime(1)
TextBox2.Text = dtTemp.ToString("MM/dd/yyyy")   ' whatever format you want

TextBox2.Text = Convert.ToDateTime(rdr("zDate")).ToString("MM/dd/yyyy")

txtEntyDate.Text = sqlRead.GetDateTime(7)尝试直接将Date分配给String,其他人首先将日期转换为字符串,并指定格式。请注意,您的代码中还有其他隐式转换,例如:

txtMemberID.Text = sqlRead.GetValue(0)

基本上,GetString以外的任何内容都要求VB对Option Strict下不允许的转换做一些猜测。