3
votes

I have the following request:

   string sqlStr = @"Select a.Text, a.TrackKey, a.NoteType, a.TranKey, a.TranType, 
    Case a.TranType when '1110' then 'https://www.cps/SAM/SO/RMA/RMAView.asp?key=' 
     when '502' then 'https://www.cps/SAM/AR/InvInqDetail.asp?Key=' 
     when '511' then 'https://www.cps/SAM/AR/InvInqDetail.asp?Key=' 
     when '801' then 'https://www.cps/SAM/AR/OrdInqDetail.asp?Key='
     when '1101' then 'https://www.cps/SAM/AP/POInqDetail.asp?key=' 
     when '1111' then 'https://www.cps/SAM/PO/TruckDetail.asp?truckkey=' 
     when '835' then 'https://www.cpsm/SAM/SO/RMA/RMAView.asp?key='  End HREF, 
    Case when a.TranType = '1110' then 'M'
     when a.TranType = '502' then 'W'
     when a.TranType = '511' then 'W' 
     when a.TranType = '1101' then 'B'
     when a.TranType = '1111' then 'B'
     when a.TranType = '835' then '('  
     when (a.TranType = '801' and a.NoteType = '9') then '{'
     when (a.TranType = '801' and a.NoteType = '8') then '(' End dataicon      
    From tamTrackrEngine a 
    Where a.CreateDate > DATEADD(DAY, DATEDIFF(DAY, 0, GetDate()), 0) 
    AND (a.EmpKey = {0} OR a.EmpKey IS NULL)
    AND a.Text IS NOT NULL
    Order by a.CreateDate DESC";

    var trackrinfo = db.Database.SqlQuery<TrackrData>(sqlStr, empKey).ToList();

    var TrackrInfo = JsonConvert.SerializeObject(trackrinfo);

    Response.Write(TrackrInfo);
    HttpContext.Response.AppendHeader("Content-Type", "application/json");
    return new EmptyResult(); 

When I run this code I get the error:

input string was not in a correct format.

I have put in a simpler sql query and gotten my results back without any problems so I think it has to do with some of the quotes in my query.

Here is my StackTrace:

[FormatException: Input string was not in a correct format.] System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) +10930058 System.String.Format(IFormatProvider provider, String format, Object[] args) +63 System.Data.Entity.Core.Objects.ObjectContext.CreateStoreCommand(String commandText, Object[] parameters) +790 System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal(String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters) +208 System.Data.Entity.Core.Objects.<>c__DisplayClass631.<ExecuteStoreQueryReliably>b__62() +45 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +333 System.Data.Entity.Core.Objects.<>c__DisplayClass631.<ExecuteStoreQueryReliably>b__61() +140 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func1 operation) +189 System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably(String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters) +335 System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery(String commandText, ExecutionOptions executionOptions, Object[] parameters) +61 System.Data.Entity.Internal.<>c__DisplayClass131.<ExecuteSqlQuery>b__12() +151 System.Lazy1.CreateValue() +415 System.Lazy1.LazyInitValue() +152 System.Lazy1.get_Value() +75 System.Data.Entity.Internal.LazyEnumerator1.MoveNext() +12 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +381 System.Linq.Enumerable.ToList(IEnumerable1 source) +58 SAM3.Controllers.SharedDataController.Trackr() in c:\Users\candicesteele\Source\Repos\SAM3\SAM3\Controllers\SharedDataController.cs:273 lambda_method(Closure , ControllerBase , Object[] ) +62 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +27 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c() +50 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e() +225 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e() +225 System.Web.Mvc.Async.<>c__DisplayClass30.b__2f(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass28.b__19() +26 System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +54 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +54 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +54 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9657896 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Source Error:

Line 273 var trackrinfo = db.Database.SqlQuery(sqlStr, empKey).ToList();Line 274:Line 275:

3
Where are you getting the exception?John Saunders
Is that the error line? If so it is the line of var trackrinfo = db.Database.SqlQuery<TrackrData>(sqlStr, empKey).ToList()user2456259
What's the value of empKey?Rahul
It is a number. A working example would be 160.user2456259

3 Answers

9
votes

It's going to be problem with the internal format. It's calling StringBuilder.AppendFormat according to that stack trace. The problem is related to your '{' in the SQL statement.

This is completely testable just by using StringBuilder.

Below blows up with

System.FormatException : Input string was not in a correct format.

var sb = new StringBuilder();
sb.AppendFormat("'{' {0}", 1);

You can remedy this by doubling up the {

var sb = new StringBuilder();
sb.AppendFormat("'{{' {0}", 1);

Works just fine.

{{ should output only a single {. Doubling it up escapes it.

1
votes

I don't have an environment to try this in, but from reading the documentation, it looks like you've got the right idea:

"You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments."

However, the place-holder in your query appears to be incorrect.

Replace {0} with @p0 in the sqlStr string and run it again.

0
votes

I'm not sure about this part:

a.EmpKey = {0}

It looks like you may have tried to build this query using string.format() at some point. In any case, that doesn't look like valid SQL to me (an int wouldn't have {/}, and if those are part of the value you're comparing against, they should be in ''s.)