0
protected void searchupdate_Click(object sender, EventArgs e)
            {
                SqlConnection con = Connection.DBconnection();
                {
                    SqlCommand com = new SqlCommand("sp_searchupdate", con);
                    com.CommandType = CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@id", txtid.Text.Trim());
                    com.Parameters.AddWithValue("@id_student", textstudentid.Text.Trim());
                    com.Parameters.AddWithValue("@tamil", txttamil.Text.Trim());
                    com.Parameters.AddWithValue("@english", txtenglish.Text.Trim());
                    com.Parameters.AddWithValue("@maths", txtmaths.Text.Trim());
                    com.Parameters.AddWithValue("@science", txtscience.Text.Trim());
                    com.Parameters.AddWithValue("@socialScience", txtsocialscience.Text.Trim());               
                    SqlParameter retval = new SqlParameter("@output", SqlDbType.VarChar, 50);
                    retval.Direction = ParameterDirection.Output;
                    com.Parameters.Add(retval);                
                    com.ExecuteNonQuery();
                    string Output = retval.Value.ToString();
                    output.Text = Output;
                    textstudentid.Text = string.Empty;
                    txttamil.Text = string.Empty;
                    txtenglish.Text = string.Empty;
                    txtmaths.Text = string.Empty;
                    txtscience.Text = string.Empty;
                    txtsocialscience.Text = string.Empty;              
            }

stored procedure:

ALTER PROCEDURE sp_searchupdate
(
@id int,
@id_student int,
@output varchar(50) output,
@Tamil Varchar (100),
@English varchar (50),
@Maths Varchar (50),
@Science Varchar (50),
@SocialScience Varchar (50) 
)
AS
IF EXISTS (SELECT * FROM studentresult WHERE id=@id) 
BEGIN
UPDATE studentresult SET Tamil = @Tamil,English = @English, Maths = @Maths,Science = @Science,SocialScience = @SocialScience WHERE id = @id
SET @output='Updated'
END
ELSE
BEGIN
INSERT INTO  studentresult  (id_student,Tamil,English,Maths,Science,SocialScience) values (@id_student,@Tamil,@English,@Maths,@Science,@SocialScience)
SET @output='Inserted'
END

when i enter input value and click update.. it shows the following error:

Insert statement conflict with foreign key fk_student. The statement terminated

I'm new to .net, can anyone help me what my mistake.. or what should i do?

Any help would be highly appreciated.. Thanks,

pcs
  • 1,864
  • 4
  • 25
  • 49
  • I have already marked your previous SAME question as a duplicate. What don't you understand from [this question?](http://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint). The error is simply telling you that you are trying to insert a value in the id_student column of the studentresult table that cannot be accepted because there is a foreign key constraint that requires a record with the same value in the linked table (probably a student names table) – Steve Nov 28 '15 at 11:45
  • @Steve: In the back end.. data's are inserted in studentresult table.. but in front end didn't work.. – pcs Nov 28 '15 at 11:50

1 Answers1

0

Most probably, you are trying to insert an ID that is not present in the primary key of the referencing table.

This kind of issue happens when bad data is passed into tables and two tables have foreign key constraint defined between them which wont allow such data to be inserted.

The rule is like this, a FK (Foreign key) cannot have a value in that column that is not also in the primary key column of the referenced table. So you might have to check on the values that you are passing through your queries.

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • Sorry, i can't understand. may i know..what should i do? – pcs Nov 28 '15 at 11:39
  • Check this field value : `id_student`. Check if the value that you are passing exists in the primary key column of the referenced table. – Dhrumil Nov 28 '15 at 11:40
  • yes.. i checked.. in the back end.. id_student ==113 in studentresult table and this same as in id==113 in student table. – pcs Nov 28 '15 at 11:49
  • yes.. but finally, i got a mistake.. in this line com.Parameters.AddWithValue("@id_student", textstudentid.Text.Trim()); checked after using breakpoint.. it shows null.. means, didn't pass any value .. may i know?.. thanks. – pcs Nov 29 '15 at 17:37
  • Your value is going as `null`. Check the value provided in the Textbox and try not using `Trim()`. – Dhrumil Nov 30 '15 at 05:07