从asp.net的文本框中的sql获取日期时间 [英] fetching datetime from sql in textbox in asp.net

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

问题描述

SqlDataAdapter da = new SqlDataAdapter("select * from VisitorPass where PassNo = " + vtxtsearch.Text.Trim(), objConn);
       DataSet ds = new DataSet();
       da.Fill(ds, "VisitorPass");
       if (ds.Tables["VisitorPass"].Rows.Count > 0)
       {
           vtxtpassno.Text = ds.Tables["VisitorPass"].Rows[0]["PassNo"].ToString();
           vtimein.Text = DateTime.Parse(vtxtdate.Text).ToString();
           vtimeout.Text = DateTime.Parse(vtxtdate.Text).ToString();
           vtxtvisitorName.Text = ds.Tables["VisitorPass"].Rows[0]["VisitorsName"].ToString();
           vtxtpurpose.Text = ds.Tables["VisitorPass"].Rows[0]["Purpose"].ToString();
           vtxttosee.Text = ds.Tables["VisitorPass"].Rows[0]["ToSee"].ToString();


       }





使用以上代码



给出错误



using above code

give error on

vtimein.Text







String was not recognized as a valid DateTime.







date format is 2015-02-10 12:59:00.000

could y pls help me out with code





亲切帮助

thx提前



kindly help
thx in advance

推荐答案

在文本框中输入的格式可能不正确。

应用验证来验证格式。
The format getting entered in textbox might be incorrect.
Apply validations for validating the format.


您应该使用 DateTime.ParseExact [ ^ ]使用指定的格式和特定​​于文化的格式信息将日期和时间的字符串表示形式转换为DateTime等效项。

或者,您可以使用 DateTime.TryParseExact [< a href =https://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]将返回一个值,指示转换是否成功。
You should use DateTime.ParseExact[^] to convert it the string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information.
Alternatively, you can use DateTime.TryParseExact[^] which will return a value that indicates whether the conversion succeeds.


vtimein.Text = ds.Tables["VisitorPass"].Rows[0]["Date"].ToString();


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

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