Can't Save NULL value through OpenAccess model

4 posts, 0 answers
  1. Lisa
    Lisa avatar
    16 posts
    Member since:
    Nov 2013

    Posted 10 Jan 2014 Link to this post

    I have a field in my database table which is an integer and allows null values. There is no default value.
    In my object model, that field shows properties of type = Int32 and Nullable = True.
    I use this code for updating the DB:

    Dim objBlend As New Blend

    objBlend = (From c In DbContext.Blends Where c.BlendID = txtBlendID_E.Text Select c).FirstOrDefault

    objBlend.SyteLineID = txtSyteline_E.Text

    DbContext.SaveChanges()

    So the user types a number in the textbox ("txtSyteline_E") and clicks the save button which invokes the code above. When the textbox has a value, it works fine. However, if the user leaves the textbox blank, because he is trying to erase the existing database value, it throws an error "Input string is not in a correct format".
    So what's the correct format for saving nulls back to the DB?

    Like most programmers, I am desperate to solve this. Any and all help is greatly appreciated.
    Thanks

  2. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 13 Jan 2014 Link to this post

    Hello Lisa,

    I suspect that the reason for the error you are receiving lies within the implicit conversion done by Visual Basic.

    When there is no value in the txtSyteline_E textbox, VB will try to convert an empty string to integer value. This is not possible and results in the exception you are experiencing. To prevent this behavior, in your code you will have to take into account the possibility that no value is provided within the textbox. The following code snippet is one example of how you could do that:
    If (String.IsNullOrEmpty(txtSyteline_E.Text)) Then
        objBlend.SyteLineID = Nothing
    Else
        objBlend.SyteLineID = txtSyteline_E.Text
    End If

    Note that it is recommended to implement some sort of validation that will assure correct values that can be converted to the required types are entered in the textboxes.

    I hope this helps.

    Regards,
    Kristian Nikolov
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  3. DevCraft banner
  4. Lisa
    Lisa avatar
    16 posts
    Member since:
    Nov 2013

    Posted 16 Jan 2014 Link to this post

    Thank you for your reply Kristian.
    It was a stupid vb.net question after all and your solution worked perfectly!
  5. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 17 Jan 2014 Link to this post

    Hi Lisa,

    I am glad you have resolved the situation. Should you have any more questions feel free to post at our forums again.

    Regards,
    Kristian Nikolov
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top