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,