在文本框中获取日期时出错 [英] error in getting date in textbox

查看:88
本文介绍了在文本框中获取日期时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将asp.net和c#与sql-server2005数据库一起使用.

我正在以mm/dd/yyyy格式在文本框中输入日期,昨天它可以正常工作,但是2day我收到错误消息:字符串对于datetime无效".

我正在使用这个:

I am using asp.net and c# with sql-server2005 database.

I am entering a date in textbox in format mm/dd/yyyy,yesterday it was working correctly but 2day i am getting error "String is not valid for datetime".

I am using this:

DateTime dt = DateTime.Parse(txtdob.Text);


并运行查询


and running query

SqlCommand cmd = new SqlCommand("insert into new_user values(''" + ID + "'',''" + txtname.Text.Trim() + "'',''" + txtcompany.Text.Trim() + "'',''" + txtdesig.Text.Trim() + "'','' " + txtcontact.Text.Trim() + " '',''" + txtemail.Text.Trim() + "'', ''" + txtadd.Text.Trim() + "'',''" + dt + "'')", con);


如果您还有其他要求,我可以为您提供.

基本问题是我想在文本框中插入生日"之类的日期并将其保存在数据库中.

请帮助我,紧急 ....

感谢


If you require anything else I can provide you.

The basic problem is I want to insert date like birthdate in textbox and want to save it in database.

Please help me, its urgent....

Thanks

推荐答案

这是一个很大的尝试,但是您最近更改了语言环境设置吗? DateTime.Parse默认使用系统语言环境设置,因此,如果您从使用mm/dd/yyyy的语言环境切换为使用其他格式(例如dd/mm/yyyy)的语言环境,则可能会导致错误您正在看到.

PS 您实际上应该使用参数化查询,以避免SQL注入!
This is a bit of a long shot, but have you changed your locale settings recently? DateTime.Parse uses the system locale settings by default, so if you switched from a locale using mm/dd/yyyy to one using some other format (e.g. dd/mm/yyyy), then that could cause the error you''re seeing.

P.S. you really should be using parameterized queries so as to avoid SQL injection!


文本框中的文本格式不正确.可能为null或不是日期时间格式
the text in textbox was not in correct form.may be it is null or may be it is not in date time format


为什么不尝试将dt转换为字符串

字符串newDate = Convert.ToString(dt);

然后另存为

SqlCommand cmd =新SqlCommand(插入new_user值(""+ ID +"'',''"+ txtname.Text.Trim()+"''," + txtcompany.Text.Trim()+ "," + txtdesig.Text.Trim()+'',''" + txtcontact.Text.Trim()+'',""+ txtemail.Text.Trim()+"' ',''"+ txtadd.Text.Trim()+"'',''"+ newDate +"''),con);
Why dont you try to convert dt to a string

string newDate=Convert.ToString(dt);

then save as

SqlCommand cmd = new SqlCommand("insert into new_user values(''" + ID + "'',''" + txtname.Text.Trim() + "'',''" + txtcompany.Text.Trim() + "'',''" + txtdesig.Text.Trim() + "'','' " + txtcontact.Text.Trim() + " '',''" + txtemail.Text.Trim() + "'', ''" + txtadd.Text.Trim() + "'',''" + newDate + "'')", con);


这篇关于在文本框中获取日期时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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