sql server - The UPDATE statement conflicted with the FOREIGN KEY constraint -


i have 3 tables in sql server:

countriestbl:

countryid int output, countryname nvarchar (50), nots nvarchar (max), modifiedby nvarchar (30) 

citiestbl:

cityid int output, cityname nvarchar (50), countryid int, nots nvarchar (max), modifiedby nvarchar (30) 

customerstbl:

customerid int output, customername nvarchar (50), customerphoto image, customeremail nvarchar(max), customerphone1 nvarchar(12), customerphone2 nvarchar(12), customeraddress nvarchar(max), customerfax nvarchar(12), customerstatus bit, countryid int, cityid int, notes nvarchar (max), modifiedby nvarchar (30) 

enter image description here

the update stored procedure looks this:

alter procedure [dbo].[update_customer_withoutpic]     @customerid int,     @customername nvarchar (50),     @customeremail nvarchar(max),     @customerphone1 nvarchar(12),     @customerphone2 nvarchar(12),     @customeraddress nvarchar(max),     @customerfax nvarchar(12),     @customerstatus bit,     @countryid int,     @cityid int,     @notes nvarchar (max),     @modifiedby nvarchar (30) begin     update customerstbl      set customername = @customername,         customeremail = @customeremail,         customerphone1 = @customerphone1,         customerphone2 = @customerphone2,         customeraddress = @customeraddress,         customerfax = @customerfax,         customerstatus = @customerstatus,         countryid = @countryid,         cityid = @cityid,         notes = @notes,         modifieddate = getdate(),         modifiedby = @modifiedby             customerid = @customerid end   

in vb, have 2 classes: data layer , business layer.

data layer - update code:

friend function update_customer_withoutpic(byval customerid string, byval customername string, byval customeremail string, byval customerphone1 string, byval customerphone2 string, byval customeraddress string, byval customerfax string, byval customerstatus boolean, byval countryid integer, byval cityid integer, byval notes string, byval modifiedby string) string             dim retval string             dim cmd new sqlcommand("update_customer_withoutpic")             cmd.parameters.addwithvalue("@customerid", customerid)             cmd.parameters.addwithvalue("@customername", customername)             cmd.parameters.addwithvalue("@customeremail", customeremail)             cmd.parameters.addwithvalue("@customerphone1", customerphone1)             cmd.parameters.addwithvalue("@customerphone2", customerphone2)             cmd.parameters.addwithvalue("@customeraddress", customeraddress)             cmd.parameters.addwithvalue("@customerfax", customerfax)             cmd.parameters.addwithvalue("@customerstatus", customerstatus)             cmd.parameters.addwithvalue("@countryid", countryid)             cmd.parameters.addwithvalue("@cityid", cityid)             cmd.parameters.addwithvalue("@notes", notes)             cmd.parameters.addwithvalue("@modifiedby", modifiedby)              retval = dm.executenonquery(cmd)              return retval         end function 

business layer:

  public function update_customer_withoutpic(byval customerid string, byval customername string, byval customeremail string, byval customerphone1 string, byval customerphone2 string, byval customeraddress string, byval customerfax string, byval customerstatus boolean, byval countryid integer, byval cityid integer, byval notes string, byval modifiedby string) string             dim retval string             retval = p.update_customer_withoutpic(customerid, customername, customeremail, customerphone1, customerphone2, customeraddress, customerfax, customerstatus, countryid, cityid, notes, modifiedby)             return retval         end function 

now update button code:

dim retval string = p.update_customer_withoutpic(txtcustomercode.text, txtcustomername.text, txtcustomeremail.text, txtcustomerphone1.text, txtcustomerphone2.text, txtcustomeraddress.text, txtcustomerfax.text, checkbox2.checked, combocustomercountry.selectedvalue, combocustomercity.selectedvalue, txtcustomernote.text, frmmain.lbluserid.text) 

i error:

the update statement conflicted foreign key constraint "fk_customerstbl_countriestbl". conflict occurred in database "alwaleedsssystem", table "dbo.countriestbl", column 'countryid'.

it seems have foreign key constraint customer table pointing @ countries table , countryid selecting in country combo box not exist in country table. make sure loading country ids same table customer table referencing.

you should reading regarding constraints , database referential integrity. https://technet.microsoft.com/en-us/library/ms175464(v=sql.105).aspx


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -