locked
Null data column returns input string was not in a correct format error RRS feed

  • Question

  • User-242280562 posted

    I am working on an event calendar in which time values (start, end) are not always required and are often Null. I'd like the format for times that are entered to read hh:mm tt. I have the code check for null values using the following if statement:

    <td>
        @If item.StartTime IsNot Nothing Then
            @item.StartTime.ToString("h:mm tt")
        Else
            @Html.DisplayFor(Function(modelItem) item.StartTime)
        End If
    </td>

    still get the following error espite the if statement:

    [FormatException: Input string was not in a correct format.]
    Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +224
    Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +92
    
    [InvalidCastException: Conversion from string "h:mm tt" to type 'Integer' is not valid.]
    Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +248
    ASP._Page_Views_EaglesEvents_Index_vbhtml.Execute() in C:\Users\Iluvw\Documents\Visual Studio 2017\Projects\MVC Projects\Eagles3555\Eagles3555\Views\EaglesEvents\Index.vbhtml:49
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
    System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
    System.Web.WebPages.StartPage.RunPage() +17
    System.Web.WebPages.StartPage.ExecutePageHierarchy() +73
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
    System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
    System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
    System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
    System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +198
    System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
    System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
    System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
    System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
    System.Web.<>c__DisplayClass7_0.<InvokeEndHandler>b__0() +32
    System.Web.StepInvoker.Invoke(Action executionStep) +107
    System.Web.<>c__DisplayClass4_0.<Invoke>b__0() +23
    Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule.OnExecuteRequestStep(HttpContextBase context, Action step) +64
    System.Web.<>c__DisplayClass284_0.<OnExecuteRequestStep>b__0(Action nextStepAction) +56
    System.Web.StepInvoker.Invoke(Action executionStep) +91
    System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar) +123
    System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +128

    The line highlighted in red is @item.StartTime.ToString("h:mm tt"). What did I do wrong?

    Friday, May 21, 2021 12:06 AM

All replies

  • User287926715 posted

    Hi Shortstuff81000,

    There are many reasons for the error FormatException, you can refer to this document.

    Then what type of StartTime attribute is, can you provide the code of the model and test data?

    Best Regards,

    ChaoDeng


     

    Friday, May 21, 2021 5:26 AM
  • User-474980206 posted

    Item.StartTime is numeric type and the format string is invalid for a numeric. 

    Friday, May 21, 2021 2:34 PM