Windows窗体中的datetimetoshort问题 [英] problem with datetimetoshort in windows form

查看:117
本文介绍了Windows窗体中的datetimetoshort问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个标签是Windows窗体.

label1用于显示成员的EnteredDate,label2用于显示UpdateDate,

我在db中插入date作为datetime.toshortstring()仅获得日期.
这些标签使用datareader从db获取值.
同时显示时,我使用了datetime.toshortstring();

现在我得到的问题是除非成员已更新,否则Updatedate为null.

因此,当在更新日期为null的成员的datagridview中单击任何行时,系统崩溃,提示日期时间格式的字符串无效.

如果没有toshortstring函数,则还会显示不需要的时间.

我在sql数据库中使用数据类型作为Date.

我只需要在存储在数据库中的标签中显示日期即可.

任何提示将不胜感激.

在此先谢谢您

I have two labels a windows form.

label1 is for displaying EnteredDate and label2 for UpdatedDate for a member,

i inserted date in db as datetime.toshortstring() to get only date.
these label get values from db using datareader.
while displaying also i used datetime.toshortstring();

Now problem i got is Updateddate is null unless the member is updated.

so when any row is clicked in datagridview of member whose updated date is null, system crashed saying invalid string for datetime format.

without toshortstring function, time is also displayed which is not required.

i used datatype in sql database as Date.

i need only date to be displayed in label which is stored in database.

any hints would be appreciated.

thanks in advance

推荐答案

重新阅读时,请检查日期是否为空值.如果为null,则将tmpStrDate(定义为字符串)设置为null,否则将日期放在此字段中.使用该字符串值来输入您的数据集.例如

When you read it back in, examine the date for a null value. If it is null set a tmpStrDate (defined as a string) to null, otherwise put the date in this field. Use that string value to feed into your datatset. e.g.

	if (reader->IsDBNull(4) == false)
	{
	    tmp_strValidTo = reader->GetDateTime(4).ToShortDateString();
        }
	else
	{	
	   tmp_strValidTo = nullptr;
	}
<pre>


您正在使用哪个版本的SQL?
which version of SQL you are using?


以正常的DateTime格式将日期保存到数据库中.并绑定到DataGridView列时(或根据需要)转换为Date.ToShortString().
Save date to database in normal DateTime format. And convert to Date.ToShortString() while binding to DataGridView column (or as you needed).


这篇关于Windows窗体中的datetimetoshort问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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