格式异常抛出“无法将参数值从String转换为DateTime” [英] Format Exception throwing "Failed to convert parameter value from a String to a DateTime"

查看:109
本文介绍了格式异常抛出“无法将参数值从String转换为DateTime”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我面临的一些问题是日期时间没有保存在数据库中。是什么钍宝宝

无法将参数值从String转换为DateTime这个例外



Hi guys
I am facing some problem is when date time is not saved in DB .its thorws
Failed to convert parameter value from a String to a DateTime this exception

SqlParameter LastLogIn = new SqlParameter("@LastLogIn", objUsers.LastLogIn);
                    LastLogIn.Direction = System.Data.ParameterDirection.Input;
                    LastLogIn.DbType = System.Data.DbType.DateTime;
                    sqlParams[5] = LastLogIn;





1。在DB数据类型中也是DateTime

2.button click事件

objBusinessUI.LastLogIn = DateTime.Now;

当前时间我要显示..但它的抛出异常



请帮助



Thnxxxx提前



1.In DB datatype is also DateTime
2.button click event
objBusinessUI.LastLogIn = DateTime.Now;
current time i wanna show..but its throws exception

Please help

Thnxxxx in advance

推荐答案

Hi Indrajeet,



传递给数据库的日期时间值是多少?

即使列是日期时间类型它可能不接受所有日期格式。



更多来自Dateteime.Now的值应转换为字符串。这可能是你收到的例外而不是实际的日期插入问题。



检查这个 http://msdn.microsoft.com/en-us /library/8kb3ddd4.aspx [ ^ ]了解更多关于Datetime.Now的用法。



您还可以调试并检查分配给objBusinessUI.LastLogIn的值,然后通过SSMS手动插入该值可以轻松追踪问题。
Hi Indrajeet,

What is the datetime value passed to the DB?
Even though the column is of Datetime type it may not accept all dateformats.

More over value from Dateteime.Now should be converted to string. This might the exception you are receiving not actual date insert issue.

Check this http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^] for more about Datetime.Now usage.

Also you can debug and check the value assigned to objBusinessUI.LastLogIn and then manually insert that value via SSMS and can trace the probelm easily.


SqlParameter LastName = new SqlParameter(@ LastName,objUsers.LastName);

LastName.Direction = System.Data.ParameterDirection .Input;

LastName.DbType = System.Data.DbType.String; //实际上我在这里弄错了.datatype i chhose --------------------------------------- ------- datetime而不是字符串,因为它抛出异常

sqlParams [2] = LastName;
SqlParameter LastName = new SqlParameter("@LastName", objUsers.LastName);
LastName.Direction = System.Data.ParameterDirection.Input;
LastName.DbType = System.Data.DbType.String; //Actually i make mistake here.datatype i chhose ----------------------------------------------datetime instead of String thats y its throws exception
sqlParams[2] = LastName;


我真的非常感谢尝试的努力帮助我
I really thanxx appreciate the efforts who try to help me


这篇关于格式异常抛出“无法将参数值从String转换为DateTime”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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