Results 1 to 8 of 8

Thread: Insert null in database

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    52

    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:
    1. 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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,433

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    52

    Re: Insert null in database

    I'm still getting problems, I tried the above this way:
    VB Code:
    1. 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

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,044

    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:
    1. If Not txtArticle_id.Text.Equals("") THEN
    2. row.article_id = CType(txtArticle_id.Text, Integer)
    3. End If
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,433

    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.

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,044

    Re: Insert null in database

    Quote Originally Posted by jmcilhinney
    Have you allowed nulls to be inserted in that column?
    He/she specified it in his/her first post...

    Quote Originally Posted by Nikske
    I have a table with a decimal field that allowes null-values.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    52

    Re: Insert null in database

    I'm a he for the record

    I'm making progress:
    VB Code:
    1. If Not Trim(CType(txtNaam.Text, String)).Length = 0 Then 'verplicht
    2.                         row.naam = CType(txtNaam.Text, String)
    3.                     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?

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,433

    Re: Insert null in database

    Quote 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
  •  



Click Here to Expand Forum to Full Width