C# 允许用C中的DateTime.Parse(date.text)方法传递空值#
我有以下表格:C# 允许用C中的DateTime.Parse(date.text)方法传递空值#,c#,datetime,webforms,datetime-parsing,C#,Datetime,Webforms,Datetime Parsing,我有以下表格: 单击按钮后,其工作方式如下,所有上述参数将传递给GetData方法 protected void btnShow_Click(object Sender, EventArgs e) { ShowReport(); } private void ShowReport() { //Reset ReportViewer1.Reset(); //DataSource DataTable dt = GetData(type.Tex
单击按钮后,其工作方式如下,所有上述参数将传递给
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:未将字符串识别为有效的日期时间
显然,将无法解析的值传递给
DateTime
将使用DateTime引发异常。Parse
因此使用DateTime.TryParse
:
DateTime.TryParse(String,DateTime)方法与DateTime.Parse(String)方法类似,只是如果转换失败,TryParse(String,DateTime)方法不会引发异常
资料来源:
用法示例:
DateTime d2;
bool success = DateTime.TryParse(date.Text, out d2);
//if successful, d2 will be set to the value of the string.
显然,将无法解析的值传递给
DateTime
将使用DateTime引发异常。Parse
因此使用DateTime.TryParse
:
DateTime.TryParse(String,DateTime)方法与DateTime.Parse(String)方法类似,只是如果转换失败,TryParse(String,DateTime)方法不会引发异常
资料来源:
用法示例:
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);
然后这样称呼它:
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);
按以下方式更改您的方法:
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);
然后这样称呼它:
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());
您可以使用此扩展:
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());
我认为date参数是一个空字符串。请改用
DateTime.TryParse
并验证您的输入!您可能想考虑使用@ RICNO方法作为TyyPARSE,一旦我尝试认为DATE参数是空字符串。使用<代码> DATETIME。TyPARSE < /Cord>代替并验证您的输入!当我尝试添加这个错误<代码>时,您可能想考虑使用@ RICNO方法作为Tr> >“方法‘tryPARSE’没有超载”1个参数,因为它需要两个参数,<代码>字符串值转换,以及<代码> DATETIME> /COD>对象,将更新我的答案。您需要传递更多的参数。阅读文档。您使用的是什么框架?4.0? 这是元数据中的方法:publicstaticbooltryparse(字符串s,out-DateTime结果)@VJPPaz-4.0,有什么问题?我的示例用法说明了一个字符串值和一个日期作为out
参数。为方法“TryParse”获取此错误无重载需要1个参数
,因为它需要两个参数,要转换的字符串
值和日期时间
对象将更新我的答案。您需要传递更多参数。阅读文档。您使用的是什么框架?4.0? 这是元数据中的方法:publicstaticbooltryparse(字符串s,out-DateTime结果)@VJPPaz-4.0,有什么问题?我的示例用法说明了字符串值和日期作为out
参数。'string'不包含'TryGetDateTime'的定义,并且找不到接受'string'类型的第一个参数的扩展方法'TryGetDateTime'(是否缺少using指令或程序集引用?)
添加后获取此错误this@kez:这是一本书。因此,您需要将其放入一个可以从任何地方使用的静态类中。“string”不包含“TryGetDateTime”的定义,并且找不到接受“string”类型的第一个参数的扩展方法“TryGetDateTime”(是否缺少using指令或程序集引用?)
添加后获取此错误this@kez:这是一本书。因此,您需要将其放入一个可以从任何地方使用的静态类中。使您的存储过程参数具有默认值NULL。让我们。使您的存储过程参数具有默认值NULL。让我们。