Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework SQL Exception: The supplied value is not a valid instance of data type float

Tags:

I have an application using the Entity Framework with a SQL Server 2008 (Express) database. I'm getting an intermittent error while doing an update to an entity in the database which indicates that 'the supplied value is not a valid instance of data type float'. However, as best I can tell, the values it's setting will always be floats. They're cast from integers, but even still will always yield floats. If the code did manage to create an invalid float somehow, I would have thought that .NET would complain about it before it even got to SQL Server.

I've included the full exception, code extracts, and schema below.

Is there anything I could be missing here - for example, could a single value be considered a float in .NET but not in SQL Server? Alternatively, is there any way I can programmatically log the precision and scale of a float so that I can diagnose what's going on if the problem comes up again?

I've added some additional logging to try to capture exactly what's going on here, but this is an intermittent problem and I can't reproduce it myself.

The error 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 

Here's the code that is being executed. (Note that the Image class is the entity class from Entity Framework.)

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;    _dataContext.SaveChanges(); } 

The schema extract for the table that this is referring to is:

CREATE TABLE Image (     ImageID uniqueidentifier NOT NULL PRIMARY KEY,     X float NOT NULL,     Y float NOT NULL ) 

Also I should note that the EF model uses the data type Single for the X and Y columns.

like image 626
John Avatar asked Sep 07 '11 23:09

John


1 Answers

I got this exact same error. It turns out I was dividing a float by zero. This didn't throw an exception in the code at the time I did the division; when I tried to save this value in the database I got the error.

Hope this is useful to some of you reading the question - Thanks John for raising it.

like image 122
Mark Avatar answered Sep 17 '22 17:09

Mark