This is a migrated thread and some comments may be shown as answers.

Can't Save NULL value through OpenAccess model

3 Answers 51 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lisa
Top achievements
Rank 1
Lisa asked on 10 Jan 2014, 08:28 PM
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

3 Answers, 1 is accepted

Sort by
0
Kristian Nikolov
Telerik team
answered on 13 Jan 2014, 09:29 AM
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!
0
Lisa
Top achievements
Rank 1
answered on 16 Jan 2014, 04:12 PM
Thank you for your reply Kristian.
It was a stupid vb.net question after all and your solution worked perfectly!
0
Kristian Nikolov
Telerik team
answered on 17 Jan 2014, 08:22 AM
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!
Tags
General Discussions
Asked by
Lisa
Top achievements
Rank 1
Answers by
Kristian Nikolov
Telerik team
Lisa
Top achievements
Rank 1
Share this question
or