Search code examples
c#.netazure-sql-databaseinternal-server-errormobileserviceclient

Azure SQL C# Backend Querying Float Caues "Internal Server Error"


I have a pretty bazar one for you guys that I just can't figure out or even wrap my head around. I've created a few Azure databases in the past and I believe this is my second one using a .NET backend. I seem to have everything pulling/inserting correctly except for floating point variables.

Condensed class client side:

class MealItem
{
    public string ID { get; set; }
    public string Name { get; set; }
    public float Price { get; set; }
}

Condensed class backend side:

public class MealItem : EntityData
{
    public string Name { get; set; }
    public float Price { get; set; }
}

And on the server side, the Price column (and other floats of the class) are set to the Data Type "float" with nulls allowed.

I am using sync tables, so the error is shown running this line:

await client.GetSyncTable<MealItem>().PullAsync("All", client.GetSyncTable<MealItem>().CreateQuery(), 
CancellationToken.None);

I also tried this for the heck of it:

await client.GetTable<MealItem>().ToListAsync();

Everything works when I comment out the floating point variables on both the client and backend. I've spent a good amount of time on this one and can't seem to figure it out. Any ideas would be greatly appreciated! Thanks!


Solution

  • Foreshadowing While continuing to search for my problem, I looked at my database in SSMS and noticed that my "float" was taking 8 bytes.

    The steps I took leading up to finding the issue were as follows. First, on the backend, I logged a Query().toString(); to get the SQL string being sent to the SQL database. Something like:

    SELECT[Extent1].[Id] AS [Id],[Extent1].[Name] AS [Name],[Extent1].[ItemType] AS [ItemType], [Extent1].[Price] AS [Price],[Extent1].[Version] AS [Version],[Extent1].[CreatedAt] AS [CreatedAt],[Extent1].[UpdatedAt] AS [UpdatedAt],[Extent1].[Deleted] AS [Deleted]FROM [dbo].[MealItems] AS [Extent1]
    

    I tried logging the result of this statement as an error but got nothing. Trying to poke around with the solution @albert Morillo posted, I tried

    SELECT[Extent1].[Id] AS [Id],[Extent1].[Name] AS [Name],[Extent1].[ItemType] AS [ItemType], Try_convert(float,[Extent1].[Price]) AS [Price],[Extent1].[Version] AS [Version],[Extent1].[CreatedAt] AS [CreatedAt],[Extent1].[UpdatedAt] AS [UpdatedAt],[Extent1].[Deleted] AS [Deleted]FROM [dbo].[MealItems] AS [Extent1]
    

    but still got no result. I finally had the bright of putting this statement in a try catch and logging the error there. It spat back the following:

    "Error: The 'Price' property on 'MealItem' could not be set to a 'System.Double' value. You must set this property to a non-null value of type."
    

    Not knowing what this meant, I looked for a double column type in SSMS. Not finding one, I decided to change my floats to doubles on the backend app. Magically, this seems to have done the trick.

    I'm not sure if this is the proper solution, but it appears to work for me. Makes sense though since the SQL database is saving and 8 byte number and a C# double is 8 bytes.