none
Arithmetic overflow error for data type tinyint RRS feed

  • Question

  • I am working on an MVC intranet application to track committees and boards.  I have a column in the board table that is of data type tinyint and the field defined in the model is of type byte.  When I try to save a board, I get the error "Arithmetic overflow error for data type tinyint, value = 256.  The statement has been terminated."

    I have debugged the application, stepping through the code that generates the error and the value being set for the MemberMax column is 7, but the error states that it is set to 256.  Does anyone have any ideas how to correct this?

    /* controller code */
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // get the board name for the search
                    string boardName = collection.Get("BoardName");
                    // get values that must be converted first
                    byte mbrMax = Convert.ToByte(collection.Get("MemberMax"));
    
                    // check to see if this name is already in the database
                    var dupCheck = from b in bdContext.Board
                                   where b.BoardName.Equals(boardName)
                                   select b;
    
                    // check to see if this board name has already been used
                    if (dupCheck.Count() == 0)
                    {
                        // create a new record
                        Board newBoard = new Board();
    
                        // generate a Guid for the record
                        newBoard.BoardID = System.Guid.NewGuid();
                        // change the name to title case
                        newBoard.BoardName = textInfo.ToTitleCase(boardName);
                        // if Dekalb is in the string, change it to DeKalb
                        if (newBoard.BoardName.Contains("Dekalb")) newBoard.BoardName.Replace("Dekalb", "DeKalb");
    
                        // get the remaining values
                        if (collection.Get("Active") == "true,false") newBoard.Active = true;
                        else newBoard.Active = false;
                        newBoard.MemberMax = mbrMax;
                        newBoard.Purpose = collection.Get("Purpose");
                        newBoard.Requirements = collection.Get("Requirements");
    
                        // add and save the record
                        bdContext.Board.Add(newBoard);
                        bdContext.SaveChanges();
    
                        // redirect to have the post information filled in
                        return RedirectToAction("Board", "Details", newBoard.BoardID);
                    }
                        // if the name has been used, show the details to the user
                    else { return RedirectToAction("Details", dupCheck.First().BoardID); }
                }
                catch (Exception ex)
                {
                    // inspect the message (for debugging purposes)
                    string message = ex.Message;
                    return View();
                }
            }
    
    /* model code */
        public class Board
        {
            [Key]
            public Guid BoardID { get; set; }
            
            [MaxLength(150)]
            [DisplayName("Board Name")]
            public string BoardName { get; set; }
            public string Purpose { get; set; }
            [DisplayName("Max Members")]
            public byte MemberMax { get; set; }
            public string Requirements { get; set; }
            public string Removal { get; set; }
            public byte[] ByLaws { get; set; }
            public byte[] Attachment1 { get; set; }
            public byte[] Attachment2 { get; set; }
            public bool Active { get; set; }
    
            public string IsActive
            {
                get
                {
                    if (Active == true) return "Yes";
                    else return "No";
                }
            }
    



    .NET Developer, SQL Developer, System Admin

    Monday, June 3, 2013 6:13 PM

Answers

  • Hi Chris,

    I change the type of MemberMax property to byte in the application, but unfortunately I didn't get the same exception.

    If you have modified the type of this property manually, have you tried to use Code First Migration to update the database?

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ole88 Monday, June 17, 2013 2:06 PM
    Thursday, June 13, 2013 7:22 AM
    Moderator

All replies

  • Hi ole88,

    Unfortunately it is a little difficult to reproduce the same problem on our machines?

    Would you like to upload a simple project to SkyDrive and share the link here? We could have a look to get better understanding.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 4, 2013 7:02 PM
    Moderator
  • Debug your code very carefully, lots of time when I had the same issue, I was passing a wrong argument (maybe it could be wrong column value or something like this).

    I'm pretty sure if you test carefully you'll find a very silly mistake.


    Please Mark as Answer and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.me/


    Wednesday, June 5, 2013 1:13 PM
  • Sorry for the late reply Chester.  I have been busy and just returned to this.
    I don't have a "simple" project of this that is ready to go.  Just to fill you in.  I debugged the program and even tried changing the data type of the column in SQL Server 2008 R2 to smallint and then changed the data types accordingly in the model and controller.

    After doing that, I still get the same error even though the data type is no longer a tinyint for that column.  I will see if it will be ok for me to share the project as is.  The only thing you won't have is the existing database to work with.


    .NET Developer, SQL Developer, System Admin

    Thursday, June 6, 2013 7:57 PM
  • Thanks for answering Michael!

    If you look at my post to Chester above, you will see that I am still getting the same error while debugging even after changing the data type from tinyint to smallint.  I have stepped through the controller, line by line, trying to figure this out.  The data is coming through OK and the value is far below 256.  Could there be an update to LINQ or MVC that I need to apply for this?


    .NET Developer, SQL Developer, System Admin

    Thursday, June 6, 2013 7:59 PM
  • Hi ole88,

    Thanks for your information.

    "I debugged the program and even tried changing the data type of the column in SQL Server 2008 R2 to smallint and then changed the data types accordingly in the model and controller."

    I'm not sure how is the validation error generated if you have tried to change both database column and model class.

    "I will see if it will be ok for me to share the project as is.  The only thing you won't have is the existing database to work with."

    It will be appreciated if you can share the project to reproduce the problem.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 7, 2013 8:39 AM
    Moderator
  • Hi Chester,

    I will need your email address in order to share the project files with you.  I have uploaded an archive of the project to my SkyDrive.  As soon as I have your address, I will send a link to you.

    Thanks,
    Chris


    .NET Developer, SQL Developer, System Admin

    Friday, June 7, 2013 3:37 PM
  • Hi Chris,

    Thanks, you can email me: v-chenho at microsoft dot com.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, June 11, 2013 1:06 AM
    Moderator
  • Good Morning Chester,

    I just sent you a link to the files with a brief note.  I hope we can figure out what is causing this, it eludes me at the moment.

    Thanks,
    Chris


    .NET Developer, SQL Developer, System Admin

    Tuesday, June 11, 2013 12:27 PM
  • Hi Chris,

    Thanks, I have received the link.

    I'll post here if there are any updates.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, June 12, 2013 9:52 AM
    Moderator
  • Hi Chris,

    I change the type of MemberMax property to byte in the application, but unfortunately I didn't get the same exception.

    If you have modified the type of this property manually, have you tried to use Code First Migration to update the database?

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ole88 Monday, June 17, 2013 2:06 PM
    Thursday, June 13, 2013 7:22 AM
    Moderator
  • Hi Chester,

    I don't have CFM setup in this project.  The database already existed and was not created by the application.  That database is the result of data migration and transformation from an earlier application version.  I defined the tables and structures in the model manually.

    I'm beginning to think I have a problem with Visual Studio and may need to reinstall it.  The problem there is that I don't have the media available.  I installed it from a trial package and was later given a key to fully license it.  This isn't the first "gotcha" of some sort that I've experienced.  Any other ideas?

    Thanks,
    Chris


    .NET Developer, SQL Developer, System Admin

    Thursday, June 13, 2013 11:58 AM
  • Hi Chris,

    Thanks for the information. Sorry currently I don't have any other ideas. I'll try to involve other senior engineers in this thread. It will take some time to get the response. Your patience will be appreciated.

    I moved your thread to Entity Framework forum.

    Sorry for any inconvenience.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 14, 2013 6:34 AM
    Moderator
  • Hi Chris,

    After consulting other senior engineers, I would suggest you to create a new thread in ADO.NET, Entity Framework, LINQ to SQL, NHibernate forum on http://forums.asp.net, since it will be more suitable for ASP.NET MVC applications.

    Sorry for any inconvenience.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 17, 2013 5:04 AM
    Moderator
  • Hi Chester,

    I wanted to provide an update.  I though that it might be something disconnected between the database and the code.  This morning I tried copying the database to another server and I got the same errors.  I then enabled CFM, changed my ConnectionString to point to a non-existent database, ran Update-Database and all the problems are gone now.

    I since migrated the data, triggers, constraints and default values set on the first database and everything seems to be working fine now.

    Thanks for all your help and input!

    Chris


    .NET Developer, SQL Developer, System Admin

    Monday, June 17, 2013 2:05 PM