简体   繁体   English

如何使用日期时间处理 asp.net 中的 null 值?

[英]How to handle the null value in asp.net using Datetime?

this is my code to save the student record,这是我保存学生记录的代码,

int x = clsFunctions.InsertStudent(txtStudent.Text,Convert.ToDateTime(txtContractDate.Text))

and the function definition is, function 的定义是,

public static int InsertStudent(string Name, DateTime ContractDate)
{
int isaved = 0;
SqlParameter[] sqlprm = new SqlParameter[2];

sqlprm[0] = new SqlParameter("@Name", SqlDbType.VarChar, 100);
sqlprm[0].Value = Name;

sqlprm[1] = new SqlParameter("@ContractDate", SqlDbType.Date);
sqlprm[1].Value = ContractDate;

isaved = db.ExecuteNonQueryProcedure("ProcedureName", sqlprm);
return isaved;
}

db.ExecuteNonQueryProcedure is the function to call ExecuteNonQuery()... db.ExecuteNonQueryProcedure 是 function 调用 ExecuteNonQuery()...

My question is that when I enter the value in ContractDate field, it works fine, the data is saved in database.. But when I left it empty, I got an error "String was not recognized as a valid DateTime".我的问题是,当我在 ContractDate 字段中输入值时,它工作正常,数据保存在数据库中。但是当我将其留空时,出现错误“字符串未被识别为有效的 DateTime”。 The ContractDate field in a table in database is 'Allow null'.数据库表中的 ContractDate 字段为“允许为空”。 How to handle this null value through this code as ContractDate field is not mandatory on the form?如何通过此代码处理此 null 值,因为 ContractDate 字段在表单上不是必需的?

System.DateTime is a struct, which is non-nullable. System.DateTime是一个不可为空的结构。 I suggest you use String.IsNullOrEmpty(txtContractDate.Text) before attempting to use Convert.ToDateTime so you can know if a blank string is passed in or not and change the parameter DateTime ContractDate to DateTime? ContractDate我建议您在尝试使用Convert.ToDateTime之前使用String.IsNullOrEmpty(txtContractDate.Text) ,这样您就可以知道是否传入了空白字符串并将参数DateTime ContractDate更改为DateTime? ContractDate DateTime? ContractDate which allows you to pass in null. DateTime? ContractDate允许您传入 null。 See MSDN Nullable .请参阅MSDN 可空值

DateTime? contractDate = null; 
if (!String.IsNullOrEmpty(txtContractDate.Text))
    contractDate = Convert.ToDateTime(txtContractDate.Text);
int x = InsertStudent(txtStudent.Text, contractDate);

Edit: I made a small mistake with ??, the operator expects the same types on both sides.编辑:我在 ?? 上犯了一个小错误,操作员希望双方的类型相同。 You'll need to use more verbose code.您需要使用更详细的代码。 I've ran a test successfully on SQL Server Express 2014 LocalDB using code similar as below.我已经使用类似于下面的代码在 SQL Server Express 2014 LocalDB 上成功运行了测试。

Finally, you need to handle the null case of contractDate when you create your SqlParameter using DBNull.Value as such:最后,您需要在使用DBNull.Value创建 SqlParameter 时处理 contractDate 的 null 情况,如下所示:

if (ContractDate != null)
    sqlprm[1].Value = ContractDate.Value;
else
    sqlprm[1].Value = DBNull.Value;
<asp:TextBox ID="TextBox1" TextMode="Date" Text='<%# Eval("ColumnName").ToString() == "" ? Eval("ColumnName") : Convert.ToDateTime(Eval("ColumnName").ToString()).ToString("yyyy-MM-dd") %>' runat="server" CssClass="form-control"></asp:TextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM