Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
If foreign key has value, the data is inserting. if not it showing error
error is

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Team_Poli__Distr__63D8CE75". The conflict occurred in database "Teamindia7717", table "dbo.Team_District", column 'District_Id'.
The statement has been terminated.


and i have tried setting foreign key value null and defalut value 0 but it showing still same error

my code is
C#
protected void Button1_Click(object sender, EventArgs e)
       {
           try
           {

               con.Open();


               int State_ID = Convert.ToInt32(ddlState.SelectedValue);
               int District_ID = Convert.ToInt32(ddldistrict.SelectedValue);

               int Area_ID;
               int Village_ID;
               if (ddlArea.SelectedValue == "")
               {
                    Area_ID = 0;

               }
               else
               {
                   Area_ID = Convert.ToInt32(ddlArea.SelectedValue);
               }
               if (ddlVillage.SelectedValue == "")
               {
                    Village_ID = 0;

               }
               else
               {
                   Village_ID = Convert.ToInt32(ddlVillage.SelectedValue);
               }
               SqlCommand cmd = new SqlCommand();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.CommandText = "SP_InsertPoliticalLeaders";
               cmd.Parameters.AddWithValue("@SelectPosition", ddlselectposition.SelectedItem.ToString());
               cmd.Parameters.AddWithValue("@State_Id", State_ID);
               cmd.Parameters.AddWithValue("@District_Id", District_ID);
               cmd.Parameters.AddWithValue("@Area_Id", Area_ID);
               cmd.Parameters.AddWithValue("@Village_Id", Village_ID);
               cmd.Parameters.AddWithValue("@IsPM", chkIsPM.Checked.ToString());

               cmd.Parameters.AddWithValue("@IsCM", chkIsCM.Checked.ToString());
               cmd.Parameters.AddWithValue("@IsCentralMinister", chkIsCentralMinister.Checked.ToString());
               cmd.Parameters.AddWithValue("@IsStateMinister", chkIsStateMinister.Checked.ToString());

               cmd.Parameters.AddWithValue("@IsCorporator", chkIsCorporator.Checked.ToString());
               cmd.Parameters.AddWithValue("@IsActive", chkIsActive.Checked.ToString());
               cmd.Parameters.AddWithValue("@PartyName", ddlPartyName.Text);
               cmd.Parameters.AddWithValue("@Name", txtName.Text);
               cmd.Parameters.AddWithValue("@Position", txtPosition.Text);
               cmd.Parameters.AddWithValue("@StartPeriod", txtStartPeriod.Text);
               cmd.Parameters.AddWithValue("@EndPeriod", txtEndPeriod.Text);
               cmd.Parameters.AddWithValue("@DOB", txtDob.Text);
               cmd.Parameters.AddWithValue("@PlaceOfbirth", txtPlaceOfBirth.Text);
               cmd.Parameters.AddWithValue("@Religion", ddlReligion.SelectedItem.ToString());
               cmd.Parameters.AddWithValue("@Education", txtEducation.Text);
               cmd.Parameters.AddWithValue("@PermenentAddress", txtpermanentAddress.Text);
               cmd.Parameters.AddWithValue("@OfficialAddress", txtOfficialAddress.Text);
               cmd.Parameters.AddWithValue("@MobileNumber", txtMobile.Text);
               cmd.Parameters.AddWithValue("@EmailId", txtEmail.Text);
               cmd.Parameters.AddWithValue("@Website", txtWebsite.Text);
               cmd.Parameters.AddWithValue("@Spouse", txtSpouse.Text);
               cmd.Parameters.AddWithValue("@Children", txtChildren.Text);
               cmd.Parameters.AddWithValue("@PoliticalCareer", txtPoliticalCareer.Text);
               cmd.Parameters.AddWithValue("@ImportantPositions", txtImportantPositions.Text);
               cmd.Parameters.AddWithValue("@Achievements", txtAchievements.Text);
               Guid FileName = Guid.NewGuid();
               ImageUpload.SaveAs(Server.MapPath("~/Images/" + FileName + ".png"));
               cmd.Parameters.AddWithValue("@ImagePath", "~/Images/" + FileName + ".png");

               cmd.Connection = con;

               cmd.ExecuteNonQuery();
               Label1.Text = "Inserted ";
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               con.Close();
               con.Dispose();
           }





My table is


create table Team_PoliticalLeaders
(
Id int primary key identity(1,1),
SelectPosition nvarchar(100),
State_Id int foreign key references Team_States(State_Id),
District_Id int foreign key references Team_District(District_Id),
Area_Id int foreign key references Team_Area(Area_Id),
Village_Id int foreign key references Team_Village(Village_Id),
IsPM bit,
IsCM bit,
IsCentralMinister bit,
IsStateMinister bit,
IsCorporator bit,
IsActive bit,
PartyName varchar(250),
ImagePath nvarchar(Max),
Name varchar(150),
Position varchar(500),
StartPeriod nvarchar(100),
EndPeriod nvarchar(100),
DOB nvarchar(150),
PlaceOfBirth varchar(250),
Religion varchar(50),
Education nvarchar(500),
PermenentAddress nvarchar(500),
OfficialAddress nvarchar(500),
MobileNumber nvarchar(15),
EmailId nvarchar(100),
Website nvarchar(500),
Spouse varchar(150),
Children nvarchar(100),
PoliticalCareer nvarchar(max),
ImportantPositions nvarchar(max),
Achievements nvarchar(max),
)
Posted
Updated 10-Jan-15 19:25pm
v3
Comments
Zoltán Zörgő 11-Jan-15 6:51am    
And what is your SP_InsertPoliticalLeaders stored procedure exactly doing?
Member 11052105 11-Jan-15 7:02am    
My stored procedure is


create procedure SP_InsertPoliticalLeaders
(
@SelectPosition nvarchar(100),
@State_Id int ,
@District_Id int ,
@Area_Id int,
@Village_Id int,
@IsPM bit,
@IsCM bit,
@IsCentralMinister bit,
@IsStateMinister bit,
@IsCorporator bit,
@IsActive bit,
@PartyName varchar(250),
@ImagePath nvarchar(Max),
@Name varchar(150),
@Position varchar(500),
@StartPeriod nvarchar(100),
@EndPeriod nvarchar(100),
@DOB nvarchar(150),
@PlaceOfBirth varchar(250),
@Religion varchar(50),
@Education nvarchar(500),
@PermenentAddress nvarchar(500),
@OfficialAddress nvarchar(500),
@MobileNumber nvarchar(15),
@EmailId nvarchar(100),
@Website nvarchar(500),
@Spouse varchar(150),
@Children nvarchar(100),
@PoliticalCareer nvarchar(max),
@ImportantPositions nvarchar(max),
@Achievements nvarchar(max)
)
as begin
insert into Team_PoliticalLeaders(SelectPosition,State_Id,District_Id,Area_Id,Village_Id,IsPM,IsCM,IsCentralMinister,IsStateMinister,IsCorporator,IsActive,PartyName,ImagePath,Name,Position,StartPeriod,EndPeriod,DOB,PlaceOfBirth,
Religion,Education,PermenentAddress,OfficialAddress,MobileNumber,EmailId,Website,Spouse,Children,PoliticalCareer,
ImportantPositions,Achievements)
values(@SelectPosition,@State_Id,@District_Id,@Area_Id,@Village_Id,@IsPM,@IsCM,@IsCentralMinister,@IsStateMinister,@IsCorporator,@IsActive,@PartyName,@ImagePath,@Name,@Position,@StartPeriod,@EndPeriod,@DOB,@PlaceOfBirth,
@Religion,@Education,@PermenentAddress,@OfficialAddress,@MobileNumber,@EmailId,@Website,@Spouse,@Children,@PoliticalCareer,
@ImportantPositions,@Achievements)
end

Please give me suggestion....
Zoltán Zörgő 11-Jan-15 7:07am    
Why do you use stored procedure for that? It is a point of mistake.
Still, it looks ok at first sight. Have you tried to call the same stored procedure with the same parameters from SQL Management Studio? Does that id exist in Team_District(District_Id)?
Member 11052105 12-Jan-15 1:13am    
Team_District(District_Id)

Yeah it is existed
Zoltán Zörgő 12-Jan-15 2:59am    
Not the field, the value you try to insert. Debug the code, step into the point where you pass all those parameters to the cmd object, and verify!

What you have pasted in your post is irrelevant from the problem's point of view. What we need to see is the DDL of the stored procedure you call, and of course the DDL of the tables affected.

In general if you specify foreign key constraint you have two approaches: if you allow null on the foreign key field, you can insert null, but if you don't than you need to have in that field valid related entity primary key value when you insert the record.

A common problem is when such a stored procedure is inserting in both tables at the same time. Than you need first to insert into the referred table, than into the referring one. If the referred table's primary key is of identity type, you can retrieve the newly generated value with @@IDENTITY[^] and use it in the second insert statement.
 
Share this answer
 
Your trying to either insert a duplicate DistrictId or you're not passing in the value of a valid District that already exists in the database.

To me, this sounds like the foreign key relationship on DistrictId isn't setup correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900