SQL Exception in Entity Framework: The given value is not a valid instance of the data type float.

entity-framework sql-server-2008 types


I have an application that uses SQL Server 2008 (Express) and the Entity Framework. The given value is not a valid instance of the data type float, according to an error message that I sometimes see while updating a database object. To the best of my knowledge, the values it is establishing, however, will always be floating. Despite being cast from integers, they will always result in floats. I would have expected.NET to raise an error before it reached SQL Server if the code somehow managed to produce an erroneous float.

The whole exception, code snippets, and schema are all supplied here.

Is there anything I could be overlooking here, such as the possibility that a single number might be a float in.NET but not in SQL Server? If not, is there a method I can programmatically record a float's accuracy and scale so I can figure out what's wrong if the issue reappears?

Although I've added more logs to attempt to pinpoint precisely what's happening, I haven't been able to duplicate this issue.

The mistake is:

System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. --->

System.Data.SqlClient.SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 4 ("@1"): The supplied value is not a valid instance of data type float. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.

 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
 at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
 at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
 at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
 at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
 at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
 at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
 at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
 at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
 at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
 at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
 --- End of inner exception stack trace ---
 at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
 at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
 at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
 at System.Data.Objects.ObjectContext.SaveChanges()
 at MyApplication.ImageProcessing.ProcessImage(Image image) in C:\Code\ImageProcessing.cs:line 224

The code that is being run is shown below. (Remember that the Entity Framework's Image class is an entity class.)

private static System.Random _randomLocation = new System.Random();

private void ProcessImage(Image image)
   float x = _randomLocation.Next(668); // note: the System.Random.Next method always returns an int
   float y = 0 - image.Height; // note: image.Height is an int and is always around 300-600 in value in this application

   image.X = x;
   image.Y = y;

The table that is being discussed here has the following schema extract:

    ImageID uniqueidentifier NOT NULL PRIMARY KEY,
    X float NOT NULL,
    Y float NOT NULL

Additionally, I should mention that the X and Y columns in the EF model utilize the Single data type.

9/7/2011 11:34:41 PM

Popular Answer

I had the same issue. I had been dividing a float by zero, it turned out. When I performed the division, this didn't cause the code to produce an issue; but, when I attempted to put this number in the database, I encountered a problem.

Thanks to John for bringing up the subject; I hope some of you who are reading it will find it beneficial.

1/13/2012 5:29:04 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow