1

I have a couple of textfields in a Windows form. One of these textfields is allowed to be NULL. When I enter a value for every field, it is all inserted, no problem. When I leave the field (txtGewicht) blank, I can't seem to be able to insert NULL in Access Database.

    If Double.TryParse(txtGewicht.Text, 0) Then
      klant.Gewicht = Double.Parse(txtGewicht.Text)
    Else
      klant.Gewicht = Nothing
    End If

This is what I get:

"Cannot set column 'Gewicht' to NULL, please use DBNull instead"

So I changed 'Nothing' to DBNull.value, but it then tells me that System.DBNull cannot be converted to type Double.

Origin
  • 1,943
  • 13
  • 33
Mekswoll
  • 1,355
  • 3
  • 15
  • 35

1 Answers1

0

A Double is a value type - not a reference type. Thus, it is not nullable by default.

If klant.Gewicht is declared like so:

Public Property Gewicht as Double

then it will never be "null" as far as the command builder is concerned.

Even explicitly saying:

d = nothing

has no effect, it will still be 0.0

You might try implementing your property as a Nullable(of Double).

This will allow the property to be considered "Null". I'm not sure if the CommandBuilder will pick up on this automatically, but it's a start.

Also - I find your method of Double.TryParse interesting. The point of the second argument is to store the result of the parsing (if successful) to.

Your code could be shortened to:

If Not Double.TryParse(txtGewicht.Text, klant.Gewicht) Then
      klant.Gewicht = ' Some Default Value
End If

Edit:

If you had stepped through the code in the debugger, you might have noticed that the klant.Gewicht was 0.0 (the default value) even after attempting to set it to nothing.

Origin
  • 1,943
  • 13
  • 33
  • Sorry, should have mentioned, I have my Gewicht field declared as Nullable, so I can actually set it to Nothing, but the CommandBuilder doesn't pick up on that fact when inserting it into the Database. – Mekswoll Jan 04 '12 at 01:50
  • Ah - see this thread then: http://stackoverflow.com/questions/5868313/ado-net-commandbuilder-insertcommand-and-default-constraints. I don't use command builder myself - because I found it to not save much time. I either write the simple query myself, or if it's more complex - use NHibernate. – Origin Jan 04 '12 at 01:53
  • Okay thanks, I'll have a look there. Could I ask about the parsing though. I had originally done it as you said, but then it gives me an error telling me that it doesn't allow exlpicit conversion from Double? to Double, so I put CDbl(klant.Gewicht), but if I output the value of klant.Gewicht afterwards, it hasn't changed. – Mekswoll Jan 04 '12 at 01:56
  • My apologies. I have edited the answer accordingly. The Nullable(of T) exposes a .HasValue, and a .Value member. The .Value member is read only, so it is not an acceptable Double for Parsing / casting. And - because Double? could be null, it can't directly convert the two (as the error says). Don't forget to mark the question as answered if you feel it is. If you you still can't get the conversion working properly you might ask a question specific to that and that doesn't reference Access. – Origin Jan 04 '12 at 02:00