我有一个TextBox
控件,其中包含一个日期,通常可以是null
。我在插入中使用它,当我尝试插入时我收到错误消息:
String was not recognized as a valid DateTime.
我不确定如何将null值作为日期时间传递。这是我的代码。
Convert.ToDateTime(txtDateAction.Text)
我遇到了问题。
protected void btnLog_Click(object sender, EventArgs e)
{
PhoneLogsBLL.InsertOCASPhoneCall(
Convert.ToInt16(txtStaffID.Text),
Convert.ToDateTime(txtDate.Text),
chkAdvisoryBoard.Checked,
chkClaims.Checked,
chkDR.Checked,
chkEEOICPA.Checked,
chkOCAS.Checked,
chkPhysicianPanel.Checked,
chkPC.Checked,
chkWebsite.Checked,
chkSEC.Checked,
chkCATI.Checked,
chkNIOSH800.Checked,
chkORAU800.Checked,
chkCongressional.Checked,
chkCOI.Checked,
chkDOLOutreach.Checked,
txtDiscussion.Text,
chkActionRequired.Checked,
Convert.ToDateTime(txtDateAction.Text),
txtAction.Text,
0,
chkActivityReport.Checked);
}
public static void InsertOCASPhoneCall(
short sintStaffID,
DateTime dtmDateCalled,
bool blnAB,
bool blnClaims,
bool blnDR,
bool blnEEOICPA,
bool blnOCAS,
bool blnPhysicianPanel,
bool blnPC,
bool blnWebSite,
bool blnSEC,
bool blnCATI,
bool blnNIOSH800,
bool blnORAU800,
bool blnCC,
bool blnCOI,
bool blnDOL,
string vcharDiscussion,
bool blnActionNeeded,
DateTime dtmActionTaken,
string ActionTaken,
short sintActionTakenStaffID,
bool blnActivityReport)
{
UnitOfWorkCTS uow = new UnitOfWorkCTS();
using (var repo = new OCASPhoneCallsRepository(uow))
{
OCASPhoneCalls call = new OCASPhoneCalls();
call.dtmDateCalled = dtmDateCalled;
call.sintStaffID = sintStaffID;
call.blnAB = blnAB;
call.blnClaims = blnClaims;
call.blnDR = blnDR;
call.blnEEOICPA = blnEEOICPA;
call.blnOCAS = blnOCAS;
call.blnPhysicianPanel = blnPhysicianPanel;
call.blnPC = blnPC;
call.blnWebSite = blnWebSite;
call.blnSEC = blnSEC;
call.blnCATI = blnCATI;
call.blnNIOSH800 = blnNIOSH800;
call.blnORAU800 = blnORAU800;
call.blnCC = blnCC;
call.blnCOI = blnCOI;
call.blnDOL = blnDOL;
call.vcharDiscussion = vcharDiscussion;
call.blnActionNeeded = blnActionNeeded;
call.dtmActionTaken = dtmActionTaken;
call.sintActionTakenStaffID = sintActionTakenStaffID;
call.blnActivityReport = blnActivityReport;
repo.InsertOrUpdate(call);
uow.Save();
}
}
答案 0 :(得分:7)
您的财产应标记为
DateTime? dtmActionTaken,
您应首先检查textbox
是否包含文字:
string.IsNullOrEmpty(txtDateAction.Text) ? (DateTime?)null : Convert.ToDateTime(txtDateAction.Text),
您也应该测试以下方案:如果textbox
有文字但不是有效日期,
答案 1 :(得分:0)
DateTime是一个结构,因此您无法直接将其设置为null。在应用转换器之前,尝试写入值为null或空的检查。
答案 2 :(得分:0)
我可能会做以下事情:
更改InsertOCASPhoneCall
方法签名,以便dtmActionTaken
参数为DateTime?
(或Nullable<DateTime>
,两者相当 - 第一个是语法糖)
然后,使用string.IsNullOrEmpty(txtDateAction.Text)? null:Convert.ToDateTime(txtDateAction.Text):null。
答案 3 :(得分:0)
我是否可以建议添加错误捕获,如果文本值为null,则将日期设置为今天的日期?因为没有&#39; null&#39;常识中的日期值(类似于#12:00:00AM#)。
(在VB.net中)的一些东西:
If Textbox1.Text = "" then
Textbox1.Text = Date.Now
Else
CDate (Textbox1.Text)
End if
在c#中:
if (string.IsNullOrEmpty(Textbox1.Text)) { Textbox1.Text = System.DateTime.Now; } else { Convert.ToDateTime(Textbox1.Text); }
答案 4 :(得分:0)
由于您将DateTime传递给InsertOCASPhoneCall,而不是Nullable,因此必须假定它是必需的。因此,您应该在对InsertOCASPhoneCall的调用周围放置一个if语句,以确保它具有有效值:
DateTime dtmActionTaken;
if (DateTime.Parse(txtDateAction.Text, out dtmActionTaken) == false)
{
// notify the user of the invalid date
}
else
PhoneLogsBLL.InsertOCASPhoneCall(
Convert.ToInt16(txtStaffID.Text), ...
...dtmActionTaken
如果数据库确实可以处理ActionTaken列的NULL,那么您可以改为将dtmActionTaken参数更改为InsertOCASPhoneCall to DateTime? dtmActionTaken,并在不解析时传入null。类似的东西:
DateTime dtmActionTaken;
DateTime? dtmActionTakenParm = null;
if (DateTime.Parse(txtDateAction.Text, out dtmActionTaken) == false)
{
dtmActionTakenParm = dtmActionTaken;
}
PhoneLogsBLL.InsertOCASPhoneCall(
Convert.ToInt16(txtStaffID.Text), ...
...dtmActionTakenParm
虽然你仍然可能想要通知用户它是否解析(而不是空字符串)