Results 1 to 5 of 5

Thread: Help with update using table adapter

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    3

    Help with update using table adapter

    Hi,

    I have a form with comboboxes, if the user doesnt select a value in the box I want a null to be sent to the database.

    The insert works fine, i look at the record in the db and see the null.

    The update doesnt work. gives me a n "Update statement conflicted with foreign key constaint....."

    Public Sub Update()
    TableAdapterCreate().Update(ID1, ID2, CType(cbox1.SelectedValue, Integer), CType(cbox2.SelectedValue, Integer), CType(cbox3.SelectedValue, Integer), CType(cbox4.SelectedValue, Integer), CType(cbox5.SelectedValue, Integer), CType(PostAssign.SOB.SelectedValue, Integer), CType(cbox6.SelectedValue, Integer), chkOther.Checked, Notes.Text)
    End Sub

    as I step through the selected value of the cbox 5 is shown as nothing (which it should be because the user didnt select a value for this combobox).

    I assume this needs to be a null instead of nothing to get written to the database. the combox boxes need to match a primary key in another table so sending a 0 would also result in the forieign key error since the related table does not have a 0 ID field.

    Thank you for your time and help

  2. #2
    Member
    Join Date
    Dec 2006
    Location
    Derby, UK
    Posts
    58

    Re: Help with update using table adapter

    Hi,

    You need to look at your db table - the value you are omitting is set as a required field.

    Change it to Required: No and enter a default value (0, "" etc), then add a new Update Query to your TableAdapter that omits the missing value (the default you just created will be used) and use it in your code:

    vb Code:
    1. Public Sub Update()
    2. TableAdapterCreate().Update(ID1, ID2, CType(cbox1.SelectedValue, Integer), CType(cbox2.SelectedValue, Integer), CType(cbox3.SelectedValue, Integer), CType(cbox4.SelectedValue, Integer), CType(PostAssign.SOB.SelectedValue, Integer), CType(cbox6.SelectedValue, Integer), chkOther.Checked, Notes.Text)
    3. End Sub

    HTH,

    Chris

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    3

    Re: Help with update using table adapter

    Quote Originally Posted by Chris147 View Post
    Hi,

    You need to look at your db table - the value you are omitting is set as a required field.

    Change it to Required: No and enter a default value (0, "" etc), then add a new Update Query to your TableAdapter that omits the missing value (the default you just created will be used) and use it in your code:

    vb Code:
    1. Public Sub Update()
    2. TableAdapterCreate().Update(ID1, ID2, CType(cbox1.SelectedValue, Integer), CType(cbox2.SelectedValue, Integer), CType(cbox3.SelectedValue, Integer), CType(cbox4.SelectedValue, Integer), CType(PostAssign.SOB.SelectedValue, Integer), CType(cbox6.SelectedValue, Integer), chkOther.Checked, Notes.Text)
    3. End Sub

    HTH,

    Chris

    Chris,

    The fields in the database are set to allow nulls.

    when the user inserts a new record with no values selected in the comboboxes it does work...no error record is written to the database The database records shows Null in those fields

    but....when a user updates that record, it fails, if the user selects a value in every combobox the update works but When the user leaves a combobox without a value if fails.

    My guess is that on the insert a value of null is getting sent to the database and on the update nothing is getting sent and causing the error.

    In the code I provided, is there some way to check if that selected value is nothing and if it is nothing , send a null to the database? Would that even work?

    thanks again

  4. #4
    Member
    Join Date
    Dec 2006
    Location
    Derby, UK
    Posts
    58

    Re: Help with update using table adapter

    You really need to look at your DB/TableAdapter - the Update command is clearly expecting a value to be passed to it.

    Right-Click on the TableAdapter and Add New Query - select Update and go from there.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with update using table adapter

    Foreign keys, by definition, cannot be null. If you want to allow nulls then you must remove the foreign key constraint.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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