|
-
Apr 10th, 2006, 07:36 AM
#1
Thread Starter
Member
Insert null in database
I have a table with a decimal field that allowes null-values. I want to fill this field with a value from a textbox. When I leave the textbox empty, I want to insert an null value, I'm trying it this way:
VB Code:
row.vervangartikel_id = IIf(txtVervangcode.Text.Equals(""), DBNull.Value.ToString, txtVervangcode.Text)
But this is not working, I keep getting an invalid cast error (not allowed decimal to dbnull). It does work with string-fields in the database however.
Any suggestions are welcome.
Grtz
Nik
-
Apr 10th, 2006, 07:57 AM
#2
Re: Insert null in database
The fact that you're calling ToString should have given it away. DBNull.Value is a null value, but calling ToString turns it into an empty String object, which is not the same thing. Plus, I'm guessing that your column takes numerical values, so you should NOT be assigning a String object to it at all. txtVervangcode.Text is a String, so that's wrong. You should be converting that to the appropriate type too.
-
Apr 11th, 2006, 02:27 AM
#3
Thread Starter
Member
Re: Insert null in database
I'm still getting problems, I tried the above this way:
VB Code:
row.article_id = IIf(txtArticle_id.Text.Equals(""), DBNull.Value, CType(txtArticle_id.Text, Integer))
This gives an error about DBNull cannot be converted to an Integer.
Some help would be greatly appreciated because I really need to get this fixed.
Thanks
Nik
-
Apr 11th, 2006, 02:52 AM
#4
Re: Insert null in database
I can't check it but on top off my head the ff. logic may work though it's not 100% since if you decide to update a field to null then it won'r work...
VB Code:
If Not txtArticle_id.Text.Equals("") THEN
row.article_id = CType(txtArticle_id.Text, Integer)
End If
-
Apr 11th, 2006, 03:22 AM
#5
Re: Insert null in database
Have you allowed nulls to be inserted in that column? Also, posting the exact error message is always a good idea. The precise details can be important.
-
Apr 11th, 2006, 04:06 AM
#6
Re: Insert null in database
 Originally Posted by jmcilhinney
Have you allowed nulls to be inserted in that column?
He/she specified it in his/her first post...
 Originally Posted by Nikske
I have a table with a decimal field that allowes null-values.
-
Apr 11th, 2006, 04:29 AM
#7
Thread Starter
Member
Re: Insert null in database
I'm a he for the record 
I'm making progress:
VB Code:
If Not Trim(CType(txtNaam.Text, String)).Length = 0 Then 'verplicht
row.naam = CType(txtNaam.Text, String)
End If
So when I leave the textbox empty I 'm getting an "not null allowed in field naam". This is what I wanted.
But now I have another problem with unique value's. I have an unique index on a field in the sql table. From this table I make a typed dataset. Now I can add non-unique values into the dataset. But when I try to save it to the dB I get an error that non-unique values are found. Is there a way to add an unique index on a typed dataset, so I get this error when I add non-unique values into the dataset?
-
Apr 11th, 2006, 09:00 AM
#8
Re: Insert null in database
 Originally Posted by dee-u
He/she specified it in his/her first post...
I knew that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|