0
votes

I am currently building a cross-platform mobile app using XamarinForms and an Azure Mobile app on the backend and I'm running into an issue with the InsertAsync function. Whenever I call it, it successfully inserts the data into my table but immediately throws an InvalidOperation exception with the message " Error converting data type nvarchar to numeric". Any idea why its throwing this exception/any possible fixes?

My client side model(matches the server side table):

public class Bet
{
    [PrimaryKey]
    [JsonProperty(PropertyName = "Id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "Name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "endDate")]
    public int endDate { get; set; }

    [JsonProperty(PropertyName = "Description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "Payout1")]
    public string Payout1 { get; set; }

    [Version]
    [JsonProperty(PropertyName ="version")]
    public byte[] version { get; set; }

    [JsonProperty(PropertyName = "createdAt")]
    public DateTime? createdAt { get; set; }

    [JsonProperty(PropertyName = "updatedAt")]
    public DateTime? updatedAt { get; set; }

the offending code:

    public async void addBet(Bet myBet, List<UserBet> myUserBets)
    {
        IMobileServiceTable<Bet> betTable = client.GetTable<Bet>();

        myBet.ID = null;
        await betTable.InsertAsync(myBet);
    }

My table:

Name Type IS Index

id String true

Name String false

endDate Number false

Description String false

Payout1 String false

version Version false

createdAt Date false

updatedAt Date false

deleted Boolean false

1
It looks like a type mismatch. Are you sure you're correctly matching types? My guesses are payout or ID being incorrect, not having any idea about your DB schema.Dan Rediske
Added the DB schema, any ideas?Daniel
1.I've never seen "Number" type and can't seem to find any documentation on it (in SQL) - so I think you meant "Numeric". 2. I would have expected a column labeled "End Date" to contain a Date (or DateTime) type. 3. Your capitalization is inconsistent, and depending on your server settings, it could cause issues. 4. Payout is a string? --------- That being said, your error is a type mismatch; somewhere a string (or something we think looks like a string- nvarchar ) is being converted to a number and it's causing problems. I'd look at #4 above first.Dan Rediske
1. It actually is listed as a number in Azure's mobileApp tool, I do believe it is converted to a Numeric under the hood though. 2. My EndDate is a number because I'm storing it as an offset since a certain day(similar to a JulianDay - link) 3. I've tried messing around with the capitalization and have matched it, no luck though 4. Payout isn't supposed to be a number as it could be a user entered taskDaniel
Try searching for the error you found with relation to Azure Mobile App tool- here's a result which might give you some idea: social.msdn.microsoft.com/Forums/en-US/…Dan Rediske

1 Answers

0
votes

You don't say whether the model is from the server or client. Looking at the model:

  1. On the client, you should not have a deleted column
  2. The createdAt / updatedAt should be "DateTimeOffset?" type (? is to allow nullable)
  3. The version should be byte[] type
  4. ID should be Id (capitalization matters)
  5. If this is a server-side model, then you should inherit from EntityData rather than including the CreatedAt, UpdatedAt, ID, Deleted and Version fields.