字符串未被识别为vb.net中的有效日期时间 [英] string was not recognized as a valid datetime in vb.net

查看:93
本文介绍了字符串未被识别为vb.net中的有效日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在创建一个网页来输入sql server 2012的日期。



当我从visual studio运行我的应用程序时,它工作正常并保存日期时间。



但是当我在同一台机器上发布时出现错误。



在sql时安装时,机器时间设置为英语(美国)。我现在改为英语(印度)的日期格式:dd-MM-yyyy



以下是sql的参数:

Hi All,

I am creating a webpage to enter a date to sql server 2012.

When I run my application from visual studio, it works fine and saves the datetime.

But when I publish on the same machine the error shows up.

At the time of sql installtion, the machine time was set as english(US). which I have now changed to english(India) for date format: dd-MM-yyyy

Following is the parameter for sql:

daCreate.InsertCommand.Parameters.Add(New SqlParameter("@ROUTTD", DateTime.Parse(txtTargetDate.Text)))





以下是用于验证日期格式的函数,因为我还允许在文本框中输入手动日期。另外,我使用了Jquery日期时间选择器。





Following is the function used for verifying the date format as I also allow manual date entry in text box. Also, I have used Jquery datetime picker.

Public Function fnDateValidation(ByVal textfordate)
        fnDateValidation = 0

        Dim test As Date
        If Date.TryParseExact(textfordate, "dd-MM-yyyy", _
                              System.Globalization.CultureInfo.CurrentCulture, _
                              Globalization.DateTimeStyles.None, test) Then
            fnDateValidation = 1
            'TODO: ok
        Else
            fnDateValidation = 0
            'TODO: not ok
        End If
        Return fnDateValidation
    End Function





请建议。



谢谢



Pls suggest.

Thanks

推荐答案

我可能会更正,但我相信Date格式实际应该是yyyy-mm-dd和数据库结构集'date'或'verchar'也应该有效。否则您可能会遇到问题。
I may stand corrected, but i believe the Date format should actually be yyyy-mm-dd and the database structure set to ' date ' or ' verchar ' should also work. Otherwise you may have issues.


使用您的函数验证日期和更改

Use your function to validate the date and change
daCreate.InsertCommand.Parameters.Add(New SqlParameter("@ROUTTD", DateTime.Parse(txtTargetDate.Text)))






To

daCreate.InsertCommand.Parameters.Add("@ROUTTD", SqlDbType.DateTime).Value = DateTime.Parse(txtTargetDate.Text)


这篇关于字符串未被识别为vb.net中的有效日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆