如何摆脱此错误“从字符串转换日期时间时转换失败"? [英] how to get rid of this error "conversion failed when converting datetime from character string"?

查看:481
本文介绍了如何摆脱此错误“从字符串转换日期时间时转换失败"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单n上放置了一个datetimepicker,命名为F_B1.
如下初始化变量

string f_date = F_B1.Text;



和f_date值进入数据类型为datetime的列中,但是我遇到此错误

从字符串转换日期时间时,转换失败

解决方案

不要将其转换为字符串.代替使用DateTimePicker的Text属性,而使用Value属性-这已经是一个DateTime,因此不需要进行转换.
作为一般规则,除非您必须执行以下操作,否则请不要进行转换:在实际输出到屏幕或打印机之前,必须使用DateTime值-它们更灵活,而且更小!您使用Convert.ToDateTime(f_date)吗?


i put a datetimepicker on my form n named it as F_B1.
initialize a variable as below

string f_date = F_B1.Text;



and f_date value is going in a column whose datatype is datetime but i m getting this error

conversion failed when converting datetime from character string

解决方案

Don''t convert it to a string. Instead of using the Text property of the DateTimePicker, use the Value property - this is already a DateTime so no conversion is needed.
As a general rule, never convert unless you have to: work with DateTime values until you are actually outputting to a screen or printer - they are much more flexible, as well as being smaller!


Did you use Convert.ToDateTime(f_date) ?


这篇关于如何摆脱此错误“从字符串转换日期时间时转换失败"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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