允许在 C# 中使用 DateTime.Parse(date.text) 方法传递空值

Allow to pass null values with DateTime.Parse(date.text) method in C#

我有以下表格:


一旦我点击按钮它就像这样工作,所有以上参数传递给 GetData 方法

protected void btnShow_Click(object Sender, EventArgs e)
{
     ShowReport();
}
private void ShowReport()
{
     //Reset
     ReportViewer1.Reset();

      //DataSource
      DataTable dt = GetData(type.Text, category.Text,subsidary.Text,country.Text, DateTime.Parse(date.Text));
            ............................
}

这是GetData方法

private DataTable GetData(string type, string category, string country, string subsidary, string dateHERE)
{
    // date = date.Value.ToOADate();

    DateTime? mydate = null;
    DateTime date2;
    bool check = DateTime.TryParse(dateHERE, out date2);
    if (check)
    {
        mydate = date2;
    }

    DataTable dt = new DataTable();
    string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["AB_ReportEntities"].ConnectionString;
    using (SqlConnection cn = new SqlConnection(connStr))
    {

        SqlCommand cmd = new SqlCommand("FindIncomplete_Products", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = type;
        cmd.Parameters.Add("@category", SqlDbType.NVarChar).Value = category;
        cmd.Parameters.Add("@country", SqlDbType.NVarChar).Value = country;
        cmd.Parameters.Add("@subsidary", SqlDbType.NVarChar).Value = subsidary;
        cmd.Parameters.Add("@date", SqlDbType.Date).Value = mydate;

        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        adp.Fill(dt);
    }

    return dt;
}

当日期字段在上面的表单中有空值时,我无法得到结果,我收到以下错误:

System.FormatException: String was not recognized as a valid DateTime.

显然将无法解析的值传递给 DateTime 将使用 DateTime.Parse 抛出异常,因此请改用 DateTime.TryParse

The DateTime.TryParse(String, DateTime) method is similar to the DateTime.Parse(String) method, except that the TryParse(String, DateTime) method does not throw an exception if the conversion fails.

来源:DateTime.TryParse

用法示例:

DateTime d2;
bool success = DateTime.TryParse(date.Text, out d2);
//if successful, d2 will be set to the value of the string.

像这样改变你的方法:

private DataTable GetData(string type, string category, string country, string subsidary,string date)
{
     DateTime? mydate = null;
     DateTime date2;
     bool check = DateTime.TryParse(date, out date2);
     if (check)
     {
         mydate = date2;
     }
}

然后这样称呼它:

DataTable dt = GetData(type.Text, category.Text,subsidary.Text,country.Text, date.Text);

您可以使用此扩展程序:

public static DateTime? TryGetDateTime(this string item, IFormatProvider provider = null)
{
    if (provider == null) provider = CultureInfo.CurrentCulture;
    DateTime dt;
    bool success = DateTime.TryParse(item, provider, DateTimeStyles.None, out dt);
    if (success) return dt;
    return null;
}

然后以这种方式更改您的方法调用:

DataTable dt = GetData(type.Text, 
                       category.Text,
                       subsidary.Text,
                       country.Text, 
                       date.Text.TryGetDateTime());