Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串未被识别为有效的日期时间C#_C#_Sql Server - Fatal编程技术网

字符串未被识别为有效的日期时间C#

字符串未被识别为有效的日期时间C#,c#,sql-server,C#,Sql Server,我创建了一个web应用程序,它在localhost上运行良好,但是当我上传web应用程序时,我的日历上出现了一个错误 字符串未被识别为有效的日期时间 它在(1/12)的日期范围内工作正常,但是当我选择(13/31)之间的日期时,我得到了这个错误 这就是所选择的(13-09-2016) 我的C#page 使用而不是转换。ToDateTimeweb服务器上的显示为US,并且您的文本的格式不是月在日之前-它推断的是13个月,这不是有效日期 您应该以与代码后面相同的方式解析日期: DateTime.Pa

我创建了一个web应用程序,它在
localhost
上运行良好,但是当我上传web应用程序时,我的日历上出现了一个错误

字符串未被识别为有效的日期时间

它在(1/12)的日期范围内工作正常,但是当我选择(13/31)之间的日期时,我得到了这个错误

这就是所选择的(
13-09-2016

我的C#page

使用而不是转换。ToDateTime

web服务器上的显示为US,并且您的文本的格式不是月在日之前-它推断的是13个月,这不是有效日期

您应该以与代码后面相同的方式解析日期:

DateTime.ParseExact(txtfrmdate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture)

为了避免系统对不同区域日期格式设置的混淆,我建议在转换类型时始终提及日期格式…

您应该验证输入日期格式的日期文本框,以便您可以使用DateTime.ParseExact()解析日期方法,否则它将是一个容易出错的代码,因为我试图理解您的代码,但我放弃了。我不明白前五行的目的是什么。@Steve好的,先生,开始的五行是在我进行研发时创建的,但我无论如何都不需要,错误就在那一行出现,我只是删除开始的五行,等待我的上级返回并再次上传页面
string strstartdate = "";
strstartdate = txtfrmdate.Text;
DateTime dt = Convert.ToDateTime(strstartdate);
string strfstartdate = dt.ToString(strstartdate);
DateTime dtnew = Convert.ToDateTime(strfstartdate);

SqlCommand cmd = new SqlCommand("csuvdaterange");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;

con.Open();

SqlParameter[] param =
{
    new SqlParameter("@logintype",com.ToString()),
    new SqlParameter("@name",lblempname.Text),
    new SqlParameter("@datefrm",DateTime.ParseExact(txtfrmdate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd")),
    new SqlParameter("@dateto",DateTime.ParseExact(txttodate.Text,"dd-MM-yyyy",CultureInfo.InvariantCulture).ToString("yyyy-MM-dd"))
};
DateTime.ParseExact(txtfrmdate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture)