RTE Input string was not in a correct format

Sep 16, 2009 at 4:20 PM

All --

Please help. 

This is a long shot, and maybe not the best place for this post (except for the fact that it is only place I know of for it), but here goes anyway...

Do you have any guess as to what might cause a RTE of "Input string was not in a correct format" on a call to myContext.SubmitChanges()?

The object being saved is complex and has several references to several other objects-- but I have viewed all properities (I think) during step-through debugging and all properties look valid.

The object does have a timestamp column which is, obviously, managed by SqlServer directly so right before SubmitChanges the timestamp column has a null but that is not a problem in other areas.

The object does use Linq To Sql Entity Base so that is why I thinking that this forum might be a good place to discuss it, get a hint, etc.

It tried using Context.Log but the Log is empty right before the call to SubmitChanges and the RTE error occurs directly on SubmitChanges so I cannot see what the Sql sent by Linq was at the time of execution. Or so it seems.

Anyway, any help is appreciated.


This is the RTE message...

System.FormatException : Input string was not in a correct format.
 at System.Data.Linq.ChangeProcessor.SendOnValidate(MetaType type, TrackedObject item, ChangeAction changeAction)
 at System.Data.Linq.ChangeProcessor.ValidateAll(IEnumerable`1 list)
 at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
 at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
 at System.Data.Linq.DataContext.SubmitChanges()
 C:\Code\Team\Ccccc\Ccccc.BusinessLayer\BusinessManagers\Generated\DddProcessingRecordManager.cs(47,0): at
 Team.Ccccc.BusinessLayer.BusinessManagers.ProcessingRecordManager.Create(Object targetEntity)
 C:\Code\Team\Ccccc\Ccccc.BusinessLayer\UnitTests\DddProcessingRecordManager_UnitTest.cs(312,0): at

This is the code that throws the RTE...

public void Create(object targetEntity)
 if (targetEntity == null)
  throw new System.ApplicationException("The given object, targetEntity, is null.");

 Team.Ccccc.BusinessLayer.BusinessEntities.DddProcessingRecord myEntity =

 using (Team.Ccccc.BusinessLayer.BusinessEntities.DataClassesContext myContext =
  new Team.Ccccc.BusinessLayer.BusinessEntities.DataClassesContext(Team.Ccccc.BusinessLayer.BusinessComponents.EntityHelper.GetConnectionString()))
  //StringWriter myWriter = new StringWriter();
  //myContext.Log = myWriter;
  //Debug.WriteLine("1='" + myWriter.ToString() + "'");

  myContext.DeferredLoadingEnabled = false;
  //Debug.WriteLine("2='" + myWriter.ToString() + "'");


  //Debug.WriteLine("3='" + myWriter.ToString() + "'");

  //This is the line that throws the RTE...
  //original... RTE "Input string was not in a correct format"...

  //test1... RTE "Input string was not in a correct format"...

  //test2... RTE "Input string was not in a correct format"...

  //Debug.WriteLine("4='" + myWriter.ToString() + "'");
  //...never written... so there seems to be no way to profile the sql sent to Linq in this case...

Do you have any suggestions, hints, guesses, speculation, etc???

Please help.

Thank you.

-- Mark Kamoski

Sep 16, 2009 at 9:23 PM

All --

I found my error, relative to the RTE noted above.

It was NOT related to the Linq To Sql Entity Base at all.

It was my error, my fault in coding.

I had a standard method in an shared interface...

object Retrieve(object targetPkId);

...and internally, in the implemention of the interface, in the actual class, it was casting to the right data type for the targetPkId. In this case, that is a cast from "object" to a "long". However, the call site was passing in a "string". Passing a "string" did not throw a CTE because a "string" is, underlying, an "object". However, the cast failed hard and without much detail. As such, all I got bubbled-out was "not in a correct format". Etc.

I had considered strongly-typing my interface as...

object Retrieve(long targetPkId);

...but that would require that all objects have a primary key of that type, which means I cannot use the interface everywhere.

I am guessing that I could solve this with Generics; but, sadly, my knowledge is sorely lacking in that area, so I do not quite know how to implement that.

I could change the interface to something like this...

object Retrieve(object targetPkId, System.Type targetType);

...but that would rather ugly and undermine my "keep the interface simple" vision.

I am still looking for the "best" solution here-- so, if you have hints then please send them along.


Thank you.

-- Mark Kamoski

Sep 19, 2009 at 1:45 PM
Edited Sep 19, 2009 at 1:50 PM

Hi Mark,

Not sure if I can help you, however you could use generics...  Where <T> below becomes the placeholds for the type

public T Retrieve<T>(T targetPkId)
    return test;

That way if you call it like this, it assumes the type you specify


int myInt = 66;
int resultInt = 0;
DateTime myDateTime;
DateTime resultDateTime;
resultInt = Retrieve(myInt); // Call function with int
resultDateTime = Retrieve(myDateTime); // Call function with DateTime
-- OR if you want to be specific, you can specify the types --
resultInt = Retrieve<int>(myInt);
resultDateTime = Retreive<DateTime>(myDateTime);