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

时间:2015-09-17 08:01:49

标签: c# datetime webforms datetime-parsing

我有以下表格:

enter image description here

一旦我点击按钮它的工作方式如下,所有上述参数都传递给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;
}

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

  

System.FormatException:String未被识别为有效的DateTime。

3 个答案:

答案 0 :(得分:2)

像这样改变你的方法:

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);

答案 1 :(得分:1)

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

  

DateTime.TryParse(String,DateTime)方法类似于DateTime.Parse(String)方法,但如果转换失败,则TryParse(String,DateTime)方法不会抛出异常。

来源:DateTime.TryParse

示例用法:

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

答案 2 :(得分:0)

您可以使用此扩展程序:

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());