简体   繁体   English

从asp.net mvc 5中的字符串转换日期时间时,转换失败

[英]conversion failed when converting datetime from character string in asp.net mvc 5

I have one project use asp.net mvc 5 and the database use SQL Server 2008, when I deploy the application in IIS server A use windows 7 its normal and work, but when I deploy in IIS server B use windows server 2008 sp 1, the application not work. 我有一个项目使用asp.net mvc 5,数据库使用SQL Server 2008,当我在IIS服务器A中部署应用程序时,使用Windows 7正常工作,但是当我在IIS服务器B中部署时,使用Windows Server 2008 sp 1,该应用程序无法正常工作。 when I input values the error is 当我输入值时,错误是

    Server Error in '/SystemPurchase' Application.

Conversion failed when converting datetime from character string.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting datetime from character string.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[SqlException (0x80131904): Conversion failed when converting datetime from character string.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +388
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +688
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4403
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +82
   System.Data.SqlClient.SqlDataReader.get_MetaData() +135
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6665229
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +6667096
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +577
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +107
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +288
   System.Data.SqlClient.SqlCommand.ExecuteReader() +302
   SystemPurchase.Models.transaction.insert() in c:\Users\Administrator.USer-PC.000\Documents\Visual Studio 2013\Projects\SystemPurchase\SystemPurchase\Models\transaction.cs:196
   SystemPurchase.Controllers.NewEntryController.Index(HttpPostedFileBase filequo1, HttpPostedFileBase filetool1, HttpPostedFileBase filequo2, HttpPostedFileBase filetool2, HttpPostedFileBase filequo3, HttpPostedFileBase filetool3, HttpPostedFileBase filequo4, HttpPostedFileBase filetool4, HttpPostedFileBase filepo, HttpPostedFileBase filetool, FormCollection abc, transaction aa) in c:\Users\Administrator.USer-PC.000\Documents\Visual Studio 2013\Projects\SystemPurchase\SystemPurchase\Controllers\NewEntryController.cs:199
   lambda_method(Closure , ControllerBase , Object[] ) +835
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +217
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +139
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c() +112
   System.Web.Mvc.Async.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e() +452
   System.Web.Mvc.Async.<>c__DisplayClass30.<BeginInvokeActionMethodWithFilters>b__2f(IAsyncResult asyncResult) +15
   System.Web.Mvc.Async.<>c__DisplayClass28.<BeginInvokeAction>b__19() +32
   System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +231
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +51
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

I input a date from a model use 从型号使用输入日期

Datetime.Now.ToString("yyyy/MM/dd H:mm:ss tt")

in server A its work and no error, but in server B its error 在服务器A中它的工作没有错误,但是在服务器B中它的错误

any ideas to resolve it ? 有什么解决的办法吗?

I would suggest you use a specific FormatProvider , like below: 我建议您使用特定的FormatProvider ,如下所示:

Datetime.Now.ToString("yyyy/MM/dd H:mm:ss tt", new CultureInfo("you culture Info"));

Here is a useful link, in which you will fund a list of all the available culture names. 是一个有用的链接,您可以在其中链接所有可用区域性名称的列表。

You may use the TryParseExact 您可以使用TryParseExact

And choose the format that you need 并选择所需的格式

For example : 例如 :

DateTime OutputDate = null;
 DateTime.TryParseExact(Datetime.Now.ToString(), "yyyy/MM/dd H:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out OutputDate);

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

相关问题 ASP.NET 4.0-从字符串转换为uniqueidentifier时转换失败! - ASP.NET 4.0 - Conversion failed when converting from a character string to uniqueidentifier! 使用动态文本框从字符串转换日期和/或时间时,ASP.NET转换失败 - ASP.NET Conversion failed when converting date and/or time from character string with Dynamic Textbox 从C#中的字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string in C# 从字符串转换日期和/或时间时,DateTime转换失败 - DateTime conversion failed when converting date and/or time from character string 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string 从字符串DateTime转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string DateTime asp.net错误:“将字符串转换为smalldatetime数据类型时转换失败” - asp.net error: “Conversion failed when converting character string to smalldatetime data type”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM